Oracle Client integration reference
Bu içerik henüz dilinizde mevcut değil.
To get started with the Aspire Oracle integrations, follow the Get started with Oracle integrations guide.
This article includes full details about the Aspire Oracle Client integration, which allows you to connect to and interact with Oracle databases from your Aspire consuming projects.
Installation
Section titled “Installation”You need an Oracle database and connection string for accessing the database. To get started with the Aspire Oracle client integration, install the 📦 Aspire.Oracle.EntityFrameworkCore NuGet package in the client-consuming project, that is, the project for the application that uses the Oracle client. The Oracle client integration registers a DbContext instance that you can use to interact with Oracle.
dotnet add package Aspire.Oracle.EntityFrameworkCore#:package Aspire.Oracle.EntityFrameworkCore@*<PackageReference Include="Aspire.Oracle.EntityFrameworkCore" Version="*" />Add Oracle client
Section titled “Add Oracle client”In the Program.cs file of your client-consuming project, call the AddOracleDatabaseDbContext extension method on any IHostApplicationBuilder to register a DbContext for use via the dependency injection container. The method takes a connection name parameter.
builder.AddOracleDatabaseDbContext<ExampleDbContext>(connectionName: "oracledb");You can then retrieve the DbContext instance using dependency injection. For example, to retrieve the connection from an example service:
public class ExampleService(ExampleDbContext context){ // Use database context...}For more information on dependency injection, see .NET dependency injection.
Enrich Oracle database context
Section titled “Enrich Oracle database context”You may prefer to use the standard Entity Framework method to obtain a database context and add it to the dependency injection container:
builder.Services.AddDbContext<ExampleDbContext>(options => options.UseOracle(builder.Configuration.GetConnectionString("oracledb") ?? throw new InvalidOperationException("Connection string 'oracledb' not found.")));You have more flexibility when you create the database context in this way, for example:
- You can reuse existing configuration code for the database context without rewriting it for Aspire.
- You can use Entity Framework Core interceptors to modify database operations.
- You can choose not to use Entity Framework Core context pooling, which may perform better in some circumstances.
If you use this method, you can enhance the database context with Aspire-style retries, health checks, logging, and telemetry features by calling the EnrichOracleDatabaseDbContext method:
builder.EnrichOracleDatabaseDbContext<ExampleDbContext>( configureSettings: settings => { settings.DisableRetry = false; settings.CommandTimeout = 30 // seconds });The settings parameter is an instance of the OracleEntityFrameworkCoreSettings class.
Properties of the Oracle resources
Section titled “Properties of the Oracle resources”When you use the WithReference method to pass an Oracle server or database resource from the AppHost project to a consuming client project, several properties are available to use in the consuming project.
Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the Uri property of a resource called db1 becomes DB1_URI.
Oracle database server
Section titled “Oracle database server”The Oracle database server resource exposes the following connection properties:
| Property Name | Description |
|---|---|
Host | The hostname or IP address of the Oracle server |
Port | The port number the Oracle server is listening on |
Username | The username for authentication |
Password | The password for authentication |
Uri | The connection URI in oracle:// format, with the format oracle://{Username}:{Password}@{Host}:{Port} |
JdbcConnectionString | JDBC-format connection string, with the format jdbc:oracle:thin:@//{Host}:{Port}. User and password credentials are provided as separate Username and Password properties. |
Example connection strings:
Uri: oracle://system:p%40ssw0rd1@localhost:1521JdbcConnectionString: jdbc:oracle:thin:@//localhost:1521Oracle database
Section titled “Oracle database”The Oracle database resource inherits all properties from its parent OracleDatabaseServerResource and adds:
| Property Name | Description |
|---|---|
Uri | The connection URI in oracle:// format, with the format oracle://{Username}:{Password}@{Host}:{Port}/{DatabaseName} |
JdbcConnectionString | JDBC connection string with database name, with the format jdbc:oracle:thin:@//{Host}:{Port}/{DatabaseName}. User and password credentials are provided as separate Username and Password properties. |
DatabaseName | The name of the database |
Example connection strings:
Uri: oracle://system:p%40ssw0rd1@localhost:1521/FREEPDB1JdbcConnectionString: jdbc:oracle:thin:@//localhost:1521/FREEPDB1Configuration
Section titled “Configuration”The Aspire Oracle Entity Framework Core integration provides multiple configuration approaches and options to meet the requirements and conventions of your project.
Use a connection string
Section titled “Use a connection string”When using a connection string from the ConnectionStrings configuration section, you provide the name of the connection string when calling builder.AddOracleDatabaseDbContext<TContext>():
builder.AddOracleDatabaseDbContext<ExampleDbContext>("oracleConnection");The connection string is retrieved from the ConnectionStrings configuration section:
{ "ConnectionStrings": { "oracleConnection": "Data Source=TORCL;User Id=OracleUser;Password=Non-default-P@ssw0rd;" }}The EnrichOracleDatabaseDbContext won’t make use of the ConnectionStrings configuration section since it expects a DbContext to be registered at the point it is called.
For more information, see the ODP.NET documentation.
Use configuration providers
Section titled “Use configuration providers”The Aspire Oracle Entity Framework Core integration supports Microsoft.Extensions.Configuration from configuration files such as appsettings.json by using the Aspire:Oracle:EntityFrameworkCore key. If you have set up your configurations in the Aspire:Oracle:EntityFrameworkCore section you can just call the method without passing any parameter.
The following is an example of an appsettings.json that configures some of the available options:
{ "Aspire": { "Oracle": { "EntityFrameworkCore": { "DisableHealthChecks": true, "DisableTracing": true, "DisableRetry": false, "CommandTimeout": 30 } } }}Use inline delegates
Section titled “Use inline delegates”You can also pass the Action<OracleEntityFrameworkCoreSettings> delegate to set up some or all the options inline, for example to disable health checks from code:
builder.AddOracleDatabaseDbContext<ExampleDbContext>( "oracle", static settings => settings.DisableHealthChecks = true);or
builder.EnrichOracleDatabaseDbContext<ExampleDbContext>( static settings => settings.DisableHealthChecks = true);Configuration options
Section titled “Configuration options”Here are the configurable options with corresponding default values:
| Name | Description |
|---|---|
ConnectionString | The connection string of the Oracle database to connect to. |
DisableHealthChecks | A boolean value that indicates whether the database health check is disabled or not. |
DisableTracing | A boolean value that indicates whether the OpenTelemetry tracing is disabled or not. |
DisableRetry | A boolean value that indicates whether command retries should be disabled or not. |
CommandTimeout | The time in seconds to wait for the command to execute. |
Client integration health checks
Section titled “Client integration health checks”By default, Aspire integrations enable health checks for all services. For more information, see Aspire integrations overview.
By default, the Aspire Oracle Entity Framework Core integration handles the following:
- Checks if the
OracleEntityFrameworkCoreSettings.DisableHealthChecksistrue. - If so, adds the
DbContextHealthCheck, which calls EF Core’sCanConnectAsyncmethod. The name of the health check is the name of theTContexttype.
Observability and telemetry
Section titled “Observability and telemetry”Aspire integrations automatically set up Logging, Tracing, and Metrics configurations, which are sometimes known as the pillars of observability. Depending on the backing service, some integrations may only support some of these features. For example, some integrations support logging and tracing, but not metrics. Telemetry features can also be disabled using the techniques presented in the Configuration section.
Logging
Section titled “Logging”The Aspire Oracle Entity Framework Core integration uses the following log categories:
Microsoft.EntityFrameworkCore.ChangeTrackingMicrosoft.EntityFrameworkCore.Database.CommandMicrosoft.EntityFrameworkCore.Database.ConnectionMicrosoft.EntityFrameworkCore.Database.TransactionMicrosoft.EntityFrameworkCore.InfrastructureMicrosoft.EntityFrameworkCore.MigrationsMicrosoft.EntityFrameworkCore.ModelMicrosoft.EntityFrameworkCore.Model.ValidationMicrosoft.EntityFrameworkCore.QueryMicrosoft.EntityFrameworkCore.Update
Tracing
Section titled “Tracing”The Aspire Oracle Entity Framework Core integration will emit the following tracing activities using OpenTelemetry:
OpenTelemetry.Instrumentation.EntityFrameworkCore
Metrics
Section titled “Metrics”The Aspire Oracle Entity Framework Core integration currently supports the following metrics:
Microsoft.EntityFrameworkCore