Connect to Oracle with EF Core
이 콘텐츠는 아직 번역되지 않았습니다.
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.
Connection properties
Section titled “Connection properties”Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the Uri property of a resource called oracledb becomes ORACLEDB_URI.
Oracle server
Section titled “Oracle server”The Oracle 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 server resource and adds:
| Property Name | Description |
|---|---|
Uri | The connection URI with the database name, 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/FREEPDB1Connect from your app
Section titled “Connect from your app”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 client integration
Section titled “Install the client integration”Install the 📦 Aspire.Oracle.EntityFrameworkCore NuGet package in the client-consuming project:
dotnet add package Aspire.Oracle.EntityFrameworkCore#:package Aspire.Oracle.EntityFrameworkCore@*<PackageReference Include="Aspire.Oracle.EntityFrameworkCore" Version="*" />Add Oracle database context
Section titled “Add Oracle database context”In Program.cs, call AddOracleDatabaseDbContext on your IHostApplicationBuilder to register a DbContext for use via the dependency injection container:
builder.AddOracleDatabaseDbContext<ExampleDbContext>(connectionName: "oracledb");Resolve the DbContext instance through dependency injection. For example, to retrieve the context 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 Core method to register a database context and then enhance it with Aspire features. Register the context first:
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
DbContextconfiguration 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:
builder.EnrichOracleDatabaseDbContext<ExampleDbContext>( configureSettings: settings => { settings.DisableRetry = false; settings.CommandTimeout = 30; // seconds });The settings parameter is an instance of OracleEntityFrameworkCoreSettings.
Configuration
Section titled “Configuration”The Aspire Oracle Entity Framework Core integration provides multiple configuration approaches 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, pass the connection name to AddOracleDatabaseDbContext:
builder.AddOracleDatabaseDbContext<ExampleDbContext>("oracleConnection");The connection string is resolved from the ConnectionStrings section:
{ "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.
Use configuration providers
Section titled “Use configuration providers”The integration supports Microsoft.Extensions.Configuration from configuration files such as appsettings.json by using the Aspire:Oracle:EntityFrameworkCore key:
{ "Aspire": { "Oracle": { "EntityFrameworkCore": { "DisableHealthChecks": true, "DisableTracing": true, "DisableRetry": false, "CommandTimeout": 30 } } }}Use inline delegates
Section titled “Use inline delegates”Pass an Action<OracleEntityFrameworkCoreSettings> to configure settings inline, for example to disable health checks:
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
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. Telemetry features can 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 emits 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