.NET Aspire Oracle Entity Framework Core integration
Includes:
Hosting integration and
Client integration
Oracle Database is a widely-used relational database management system owned and developed by Oracle. The .NET Aspire Oracle Entity Framework Core integration enables you to connect to existing Oracle servers or create new servers from .NET with the container-registry.orcale.com/databse/free container image.
Hosting integration
The .NET Aspire Oracle hosting integration models the server as the OracleDatabaseServerResource type and the database as the OracleDatabaseResource type. To access these types and APIs, add the 📦 Aspire.Hosting.Oracle NuGet package in the app host project.
dotnet add package Aspire.Hosting.Oracle
For more information, see dotnet add package or Manage package dependencies in .NET applications.
Add Oracle server and database resources
In your app host project, call AddOracle to add and return an Oracle server resource builder. Chain a call to the returned resource builder to AddDatabase, to add an Oracle database to the server resource:
var builder = DistributedApplication.CreateBuilder(args);
var oracle = builder.AddOracle("oracle")
.WithLifetime(ContainerLifetime.Persistent);
var oracledb = oracle.AddDatabase("oracledb");
builder.AddProject<Projects.ExampleProject>()
.WithReference(oracledb);
.WaitFor(oracledb);
// After adding all resources, run the app...
Note
The Oracle database container can be slow to start, so it's best to use a persistent lifetime to avoid unnecessary restarts. For more information, see Container resource lifetime.
When .NET Aspire adds a container image to the app host, as shown in the preceding example with the container-registry.oracle.com/database/free
image, it creates a new Oracle server on your local machine. A reference to your Oracle resource builder (the oracle
variable) is used to add a database. The database is named oracledb
and then added to the ExampleProject
. The Oracle resource includes a random password
generated using the CreateDefaultPasswordParameter method.
The WithReference method configures a connection in the ExampleProject
named "oracledb"
. For more information, see Container resource lifecycle.
Tip
If you'd rather connect to an existing Oracle server, call AddConnectionString instead. For more information, see Reference existing resources.
Add Oracle resource with password parameter
The Oracle resource includes default credentials with a random password. Oracle supports configuration-based default passwords by using the environment variable ORACLE_PWD
. When you want to provide a password explicitly, you can provide it as a parameter:
var password = builder.AddParameter("password", secret: true);
var oracle = builder.AddOracle("oracle", password)
.WithLifetime(ContainerLifetime.Persistent);
var oracledb = oracle.AddDatabase("oracledb");
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(oracledb)
.WaitFor(oracledb);
The preceding code gets a parameter to pass to the AddOracle
API, and internally assigns the parameter to the ORACLE_PWD
environment variable of the Oracle container. The password
parameter is usually specified as a user secret:
{
"Parameters": {
"password": "Non-default-P@ssw0rd"
}
}
For more information, see External parameters.
Add Oracle resource with data volume
To add a data volume to the Oracle resource, call the WithDataVolume method:
var builder = DistributedApplication.CreateBuilder(args);
var oracle = builder.AddOracle("oracle")
.WithDataVolume()
.WithLifetime(ContainerLifetime.Persistent);
var oracledb = oracle.AddDatabase("oracle");
builder.AddProject<Projects.ExampleProject>()
.WithReference(oracledb)
.WaitFor(oracledb);
// After adding all resources, run the app...
The data volume is used to persist the Oracle data outside the lifecycle of its container. The data volume is mounted at the /opt/oracle/oradata
path in the Oracle container and when a name
parameter isn't provided, the name is generated at random. For more information on data volumes and details on why they're preferred over bind mounts, see Docker docs: Volumes.
Warning
The password is stored in the data volume. When using a data volume and if the password changes, it will not work until you delete the volume.
Add Oracle resource with data bind mount
To add a data bind mount to the Oracle resource, call the WithDataBindMount method:
var builder = DistributedApplication.CreateBuilder(args);
var oracle = builder.AddOracle("oracle")
.WithDataBindMount(source: @"C:\Oracle\Data");
var oracledb = oracle.AddDatabase("oracledb");
builder.AddProject<Projects.ExampleProject>()
.WithReference(oracledb)
.WaitFor(oracledb);
// After adding all resources, run the app...
Important
Data bind mounts have limited functionality compared to volumes, which offer better performance, portability, and security, making them more suitable for production environments. However, bind mounts allow direct access and modification of files on the host system, ideal for development and testing where real-time changes are needed.
Data bind mounts rely on the host machine's filesystem to persist the Oracle data across container restarts. The data bind mount is mounted at the C:\Oracle\Data
on Windows (or /Oracle/Data
on Unix) path on the host machine in the Oracle container. For more information on data bind mounts, see Docker docs: Bind mounts.
Hosting integration health checks
The Oracle hosting integration automatically adds a health check for the Oracle resource. The health check verifies that the Oracle server is running and that a connection can be established to it.
The hosting integration relies on the 📦 AspNetCore.HealthChecks.Oracle NuGet package.
Client integration
You need an Oracle database and connection string for accessing the database. To get started with the .NET 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
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");
Tip
The connectionName
parameter must match the name used when adding the Oracle database resource in the app host project. In other words, when you call AddDatabase
and provide a name of oracledb
that same name should be used when calling AddOracleDatabaseDbContext
. For more information, see Add Oracle server and database resources.
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
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.")));
Note
The connection string name that you pass to the GetConnectionString method must match the name used when adding the Oracle resource in the app host project. For more information, see Add Oracle server and database resources.
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 .NET 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 .NET 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.
Configuration
The .NET 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
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
The .NET 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
}
}
}
}
Tip
The CommandTimeout
property is in seconds. When set as shown in the preceding example, the timeout is 30 seconds.
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
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
By default, .NET Aspire client integrations have health checks enabled for all services. Similarly, many .NET Aspire hosting integrations also enable health check endpoints. For more information, see:
By default, the .NET 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 theTContext
type.
Observability and telemetry
.NET Aspire integrations automatically set up Logging, Tracing, and Metrics configurations, which are sometimes known as the pillars of observability. For more information about integration observability and telemetry, see .NET Aspire integrations overview. 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
The .NET 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
Tracing
The .NET Aspire Oracle Entity Framework Core integration will emit the following tracing activities using OpenTelemetry:
- OpenTelemetry.Instrumentation.EntityFrameworkCore
Metrics
The .NET Aspire Oracle Entity Framework Core integration currently supports the following metrics:
- Microsoft.EntityFrameworkCore
See also
.NET Aspire