-
I am instrumenting an application based on EF with Aspire. The DbContexts of the application have constructors with non standard signature (either parameterless or with parameters that are not DbContextOptions and Aspire doesn't seem to like it very much.
As you can imagine from the log entry above, the application is using Duende's Identity Server to handle users. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Can you show the API you are using? Are you using the Enrich methods https://learn.microsoft.com/en-us/dotnet/aspire/whats-new/preview-4#new-enrich-methods? |
Beta Was this translation helpful? Give feedback.
-
I was using the classic builder.AddSqlServerDbContext<IdentityServerConfigurationContext>("Database", ConfigureSqlServerSettings, ConfigureDbContextOptions);
static void ConfigureDbContextOptions(DbContextOptionsBuilder builder)
{
builder.UseSqlServer(sql => sql.MigrationsAssembly(MigrationAssemblyName).EnableRetryOnFailure());
}
static void ConfigureSqlServerSettings(MicrosoftEntityFrameworkCoreSqlServerSettings settings)
{
} I knew about the new Thanks a lot! |
Beta Was this translation helpful? Give feedback.
-
Enrich new methods no longer appears to be a anchor in this page 🤷 I've got an abstract public abstract class ApplicationDbContext<TContext> : DbContext, IApplicationDbContext
where TContext : DbContext, IApplicationDbContext
{
private readonly IAuthenticatedUserService authenticatedUserService;
public ApplicationDbContext(
DbContextOptions<TContext> options,
IAuthenticatedUserService authenticatedUserService)
: base(options)
{
this.authenticatedUserService = authenticatedUserService;
}
public DbSet<DomainEventReference>? DomainEventReferences { get; internal set; }
public DbSet<QueryProjectionReference>? QueryProjectionReferences { get; internal set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new DomainEventReferenceConfiguration());
modelBuilder.ApplyConfiguration(new QueryProjectionReferenceConfiguration());
this.ConfigureDatabaseModel(modelBuilder);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.AddInterceptors(new AuditDetailsSaveChangesInterceptor(this.authenticatedUserService));
optionsBuilder.AddInterceptors(new DomainEventSaveChangesInterceptor<TContext>(this.authenticatedUserService));
}
protected abstract void ConfigureDatabaseModel(ModelBuilder modelBuilder);
} And then an implementation of that abstract class that looks like this. public sealed class PizzeriaDbContext : ApplicationDbContext<PizzeriaDbContext>
{
public PizzeriaDbContext(
DbContextOptions<PizzeriaDbContext> options,
IAuthenticatedUserService authenticatedUserService)
: base(options, authenticatedUserService)
{
}
public DbSet<Pizza> Pizzas { get; set; }
public DbSet<Order> Orders { get; set; }
protected override void ConfigureDatabaseModel(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfigurationsFromAssembly(typeof(PizzeriaDbContext).Assembly);
}
} I'm trying use Aspire Postgres in my service to add the DB context like this. var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();
builder.AddNpgsqlDbContext<PizzeriaDbContext>(ServiceNames.PizzaStoreDatabase); I get the following error. The type 'Aspire.Pizzeria.Data.PizzeriaDbContext' cannot be used as type parameter 'TContext' in the generic type or method 'AspireEFPostgreSqlExtensions.AddNpgsqlDbContext<TContext>(IHostApplicationBuilder, string, Action<NpgsqlEntityFrameworkCorePostgreSQLSettings>?, Action<DbContextOptionsBuilder>?)'. There is no implicit reference conversion from 'Aspire.Pizzeria.Data.PizzeriaDbContext' to 'Microsoft.EntityFrameworkCore.DbContext'. I guess there's a way to enrich the |
Beta Was this translation helpful? Give feedback.
Can you show the API you are using? Are you using the Enrich methods https://learn.microsoft.com/en-us/dotnet/aspire/whats-new/preview-4#new-enrich-methods?