Ir al contenido
Docs Try Aspire
Docs Try

Connect to Oracle with EF Core

Esta página aún no está disponible en tu idioma.

Oracle Database logo

This page describes how consuming C# apps connect to an Oracle resource that’s already modeled in your AppHost. For the AppHost API surface — adding an Oracle server, databases, volumes, and more — see Set up Oracle in the AppHost.

When you reference an Oracle database resource from your AppHost, Aspire injects the connection information into the consuming app as environment variables. In C#, use the Aspire Oracle Entity Framework Core client integration for automatic DbContext registration, health checks, retries, and telemetry.

Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the Uri property of a resource called oracledb becomes ORACLEDB_URI.

The Oracle 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 server resource and adds:

Property NameDescription
UriThe connection URI with the database name, 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

Add the Aspire Oracle EF Core client integration to your C# consuming app to register a DbContext for Oracle Database with automatic health checks and telemetry.

Install the 📦 Aspire.Oracle.EntityFrameworkCore NuGet package in the client-consuming project:

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

In Program.cs, call AddOracleDatabaseDbContext on your IHostApplicationBuilder to register a DbContext for use via the dependency injection container:

C# — Program.cs
builder.AddOracleDatabaseDbContext<ExampleDbContext>(connectionName: "oracledb");

Resolve the DbContext instance through dependency injection. For example, to retrieve the context 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 Core method to register a database context and then enhance it with Aspire features. Register the context first:

C# — Program.cs
builder.Services.AddDbContext<ExampleDbContext>(options =>
options.UseOracle(builder.Configuration.GetConnectionString("oracledb")
?? throw new InvalidOperationException("Connection string 'oracledb' not found.")));

Using this approach, you have more flexibility:

  • Reuse existing DbContext configuration code without rewriting it for Aspire.
  • Use Entity Framework Core interceptors to modify database operations.
  • Choose not to use Entity Framework Core context pooling, which may perform better in some circumstances.

Then call EnrichOracleDatabaseDbContext to add Aspire-style retries, health checks, logging, and telemetry:

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

The settings parameter is an instance of OracleEntityFrameworkCoreSettings.

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

When using a connection string from the ConnectionStrings configuration section, pass the connection name to AddOracleDatabaseDbContext:

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

The connection string is resolved from the ConnectionStrings section:

JSON — appsettings.json
{
"ConnectionStrings": {
"oracleConnection": "Data Source=TORCL;User Id=OracleUser;Password=Non-default-P@ssw0rd;"
}
}

The EnrichOracleDatabaseDbContext method won’t use the ConnectionStrings configuration section because it expects a DbContext to already be registered.

For more information, see the ODP.NET documentation.

The integration supports Microsoft.Extensions.Configuration from configuration files such as appsettings.json by using the Aspire:Oracle:EntityFrameworkCore key:

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

Pass an Action<OracleEntityFrameworkCoreSettings> to configure settings inline, for example to disable health checks:

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 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. Telemetry features can 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 emits the following tracing activities using OpenTelemetry:

  • OpenTelemetry.Instrumentation.EntityFrameworkCore

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

  • Microsoft.EntityFrameworkCore