visit
.Net Aspire framework is used to develop cloud and production-ready distributed applications. It consists of components to handle cloud-native concerns such as Redis, Postgres, etc.
Install the following Nuget package into the subsequent project “DotnetAspireChallenge.AppHost”
dotnet add package Aspire.Hosting.Oracle
var oracle = builder.AddOracle("oracle")
.AddDatabase("oracledb");
var apiService = builder.AddProject<Projects.DotnetAspireChallenge_ApiService>("apiservice")
.WithReference(oracle);
Install the following Nuget package into the subsequent project “DotnetAspireChallenge.ApiService”
dotnet add package Aspire.Oracle.EntityFrameworkCore
builder.AddOracleDatabaseDbContext<OracleDbContext>("oracledb");
public class Customer
{
public int Id { get; set; }
[Required]
public string Title { get; set; } = string.Empty;
[Required]
public string Description { get; set; } = string.Empty;
}
public static class AspireOracleExtension
{
public static void MapOracleAspireEndpoint(this WebApplication app)
{
app.MapGet("/oracle", async (OracleDbContext oracleDbContext) =>
{
await oracleDbContext.Customer.AddAsync(new Customer()
{
Title = "[email protected]",
Description = "sukh"
});
int rows = await oracleDbContext.SaveChangesAsync();
if (rows > 0)
{
return await oracleDbContext.Customer.FirstOrDefaultAsync();
}
else
{
return null;
}
});
}
}
internal class OracleDbContext(DbContextOptions options) : DbContext(options)
{
public DbSet<Customer> CustomersPgsql => Set<Customer>();
}
app.MapOracleAspireEndpoint();
{
"Aspire": {
"Oracle": {
"EntityFrameworkCore": {
"DisableHealthChecks": true,
"DisableTracing": true,
"DisableMetrics": false,
"DisableRetry": false,
"Timeout": 30
}
}
}
}
Congratulations..!! You’ve successfully integrated the Oracle component into the .Net Aspire project.
Github Project:
Cheatsheet: