0

I am trying to add multiple DbContext instances in app, which is launched with .NET Aspire. I also want those separate contexts to have configuration available (in this case to have migration history tables in different schemas). Lastly, in Production environments, the app may be launched without .NET Aspire.

I have looked into Microsoft documentation. The problem I see here is that it looks like database configuration to DI container needs to be changed:

Before:

builder.Services.AddDbContext<ContextA>(options => options.UseSqlServer("MY_CONNECTION_STRING"), sqlServerOptions =>
{
    sqlServerOptions.MigrationsHistoryTable("__EFMigrationsHistory", ContextA.Schema);
}));

builder.Services.AddDbContext<ContextB>(options => options.UseSqlServer("MY_CONNECTION_STRING"), sqlServerOptions =>
{
    sqlServerOptions.MigrationsHistoryTable("__EFMigrationsHistory", ContextB.Schema);
}));

After:

builder.AddSqlServerDbContext<ContextA>("NAME_FROM_ASPIRE", options => 
{
    options.????? // No way for me to change required SQL server options
});

builder.AddSqlServerDbContext<ContextB>("NAME_FROM_ASPIRE", options => 
{
    options.????? // No way for me to change required SQL server options
});

There are several issues with this change:

  1. I can't do configuration to set up SQL server the way I want it.
  2. This will not work if app is not deployed using Aspire generated manifest, but instead if Aspire is only used for local development ("NAME_FROM_ASPIRE" will not really exist if Aspire is not used).

Ideally, I would only want Aspire to add SQL Server docker image with a volume, that runs on concrete port and let the app itself decide, how context should be set up (If I misconfigure in DistributedApplicationBuilder, that is on me!)

However, the following code didn't work (I can't see external ports exposed on Docker, SQL management programs can't connect to Database, .NET app says "Connection refused"):

var password = builder.AddParameter("SqlServerPassword");

var db = builder.AddSqlServer("database", password: password, port: 1434)
                .WithVolume("data")
                .AddDatabase("NAME_FROM_ASPIRE");

var app = builder.AddProject<Projects.MyApp>("app")
                 .WithReference("db");

What am I doing wrong?

2
  • A DbContext is a Unit-of-Work implementation for an ORM, Entity Framework. It's NOT a database configuration at all. You can have multiple UoWs/DbContext in an application if that makes business sense - after all the Warehouse domain and the Accounting domain aren't the same, even if the same database is used. On the other hand AddSqlServer does configure databases but does nothing about any ORM entities, migrations or mappings Commented Jun 30 at 14:54
  • It seems the real questions aren't about Aspire or even DbContext, but how to manage EF Core migrations in any project with multiple DbContexts. Commented Jun 30 at 14:55

1 Answer 1

-1

If you want separate contexts, then you will have to create those classes that derive from DbContext.

Then within your Aspire ServiceDefaults project you would tap into the IServiceCollection and do something like this.

services.AddDbContext<AbcContext>();
services.AddDbContext<DefContext>();

What adding the contexts this way does is allows you to split your code up to hit the right database.
Here is a link to train - https://learn.microsoft.com/en-us/training/modules/use-databases-dotnet-aspire-app/?source=recommendations

Sign up to request clarification or add additional context in comments.

3 Comments

That's the same as the question's code. How will this solve the problem?
If you have 2 distinct contexts you can have 2 distinct OnConfiguring of DbContextOptionsBuilder to setup your distinct optionsBuilder.UseSqlServer in your DbContext file instead of setting up the options in the AppHost program.cs file. I think it solves the problem of using the builder. I am able to hit 2 databases in my project by going this route.
No, it makes it a lot harder to change connection settings, or load them from configuration. Should you want to target another database now you'll have to rebuild your DbContext instead of just change the connection name. Never mind targeting a different database. The OP can hit different databases already. The question was about migrations, not connections

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.