콘텐츠로 이동

Oracle Client integration reference

이 콘텐츠는 아직 번역되지 않았습니다.

Oracle Database logo

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.

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.

.NET CLI — Add Aspire.Oracle.EntityFrameworkCore package
dotnet add package Aspire.Oracle.EntityFrameworkCore

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.

C# — Program.cs
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:

C# — ExampleService.cs
public class ExampleService(ExampleDbContext context)
{
// Use database context...
}

For more information on dependency injection, see .NET dependency injection.

You may prefer to use the standard Entity Framework method to obtain a database context and add it to the dependency injection container:

C# — Program.cs
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:

C# — Program.cs
builder.EnrichOracleDatabaseDbContext<ExampleDbContext>(
configureSettings: settings =>
{
settings.DisableRetry = false;
settings.CommandTimeout = 30 // seconds
});

The settings parameter is an instance of the OracleEntityFrameworkCoreSettings class.

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.

The Oracle database server resource exposes the following connection properties:

Property NameDescription
HostThe hostname or IP address of the Oracle server
PortThe port number the Oracle server is listening on
UsernameThe username for authentication
PasswordThe password for authentication
UriThe connection URI in oracle:// format, with the format oracle://{Username}:{Password}@{Host}:{Port}
JdbcConnectionStringJDBC-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:1521
JdbcConnectionString: jdbc:oracle:thin:@//localhost:1521

The Oracle database resource inherits all properties from its parent OracleDatabaseServerResource and adds:

Property NameDescription
UriThe connection URI in oracle:// format, with the format oracle://{Username}:{Password}@{Host}:{Port}/{DatabaseName}
JdbcConnectionStringJDBC 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.
DatabaseNameThe name of the database

Example connection strings:

Uri: oracle://system:p%40ssw0rd1@localhost:1521/FREEPDB1
JdbcConnectionString: jdbc:oracle:thin:@//localhost:1521/FREEPDB1

The Aspire Oracle Entity Framework Core integration provides multiple configuration approaches and options to meet the requirements and conventions of your project.

When using a connection string from the ConnectionStrings configuration section, you provide the name of the connection string when calling builder.AddOracleDatabaseDbContext<TContext>():

C# — Program.cs
builder.AddOracleDatabaseDbContext<ExampleDbContext>("oracleConnection");

The connection string is retrieved from the ConnectionStrings configuration section:

JSON — appsettings.json
{
"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.

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:

JSON — appsettings.json
{
"Aspire": {
"Oracle": {
"EntityFrameworkCore": {
"DisableHealthChecks": true,
"DisableTracing": true,
"DisableRetry": false,
"CommandTimeout": 30
}
}
}
}

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:

C# — Program.cs
builder.AddOracleDatabaseDbContext<ExampleDbContext>(
"oracle",
static settings => settings.DisableHealthChecks = true);

or

C# — Program.cs
builder.EnrichOracleDatabaseDbContext<ExampleDbContext>(
static settings => settings.DisableHealthChecks = true);

Here are the configurable options with corresponding default values:

NameDescription
ConnectionStringThe connection string of the Oracle database to connect to.
DisableHealthChecksA boolean value that indicates whether the database health check is disabled or not.
DisableTracingA boolean value that indicates whether the OpenTelemetry tracing is disabled or not.
DisableRetryA boolean value that indicates whether command retries should be disabled or not.
CommandTimeoutThe time in seconds to wait for the command to execute.

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.DisableHealthChecks is true.
  • If so, adds the DbContextHealthCheck, which calls EF Core’s CanConnectAsync method. The name of the health check is the name of the TContext type.

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.

The Aspire Oracle Entity Framework Core integration uses the following log categories:

  • Microsoft.EntityFrameworkCore.ChangeTracking
  • Microsoft.EntityFrameworkCore.Database.Command
  • Microsoft.EntityFrameworkCore.Database.Connection
  • Microsoft.EntityFrameworkCore.Database.Transaction
  • Microsoft.EntityFrameworkCore.Infrastructure
  • Microsoft.EntityFrameworkCore.Migrations
  • Microsoft.EntityFrameworkCore.Model
  • Microsoft.EntityFrameworkCore.Model.Validation
  • Microsoft.EntityFrameworkCore.Query
  • Microsoft.EntityFrameworkCore.Update

The Aspire Oracle Entity Framework Core integration will emit the following tracing activities using OpenTelemetry:

  • OpenTelemetry.Instrumentation.EntityFrameworkCore

The Aspire Oracle Entity Framework Core integration currently supports the following metrics:

  • Microsoft.EntityFrameworkCore