Unable to configure separate migrations assembly when setting up Aspire
As part of trying to convert one of my projects to use .NET Aspire, I came across this one challenge:
npgsqlOptions.MigrationsAssembly("MySolution.MyDomain.Migrations");
npgsqlOptions.MigrationsHistoryTable("migration_history");
npgsqlOptions.UseNodaTime();
The Aspire+npgsql+EF docs get me to understanding that this is available:
builder.AddNpgsqlDbContext<MyDomainDbContext>(
"my-db",
(settings) =>
{
},
(options) =>
{
}
);
Which is similar to something I was using earlier, but the problem is that I'm assuming the Aspire integration wants to have some say on configuring the underlying DB connection. Prior to Aspire, I was creating my own NpgsqlDataSourceBuilder and calling options.UseNpgsql to configure EF with it.
This feels a bit like a chicken/egg scenario in that I can get the options for something that's already been created for me. So calling UseNpgsql doesn't really work because within the 2nd lambda of AddNpgsqlDbContext, I can't get my connection information.
What's the best way forward here? :slightly_smiling_face:
Is it okay to call the UseNpgsql overload without passing connection information? Or will this be overridden/ignored somehow outside/after my lambda?
It does get ignored, it didn't help me with my own same situation. Can't get ef migrations add to work.
This worked like a charm for me:
builder.AddNpgsqlDbContext<EmployeesDbContext>(
"employees-db",
configureDbContextOptions: options => options.UseNpgsql(b => b.MigrationsAssembly(typeof(Program).Assembly.FullName)));
Sorry for not answering here earlier.
The above is missing information on the exact problem people are running into, e.g. no actual errors/exceptions. But in any case, @vladislav-karamfilov's snippet should work.
I'll go ahead and close this as it has been a while and there's nothing actionable at this point, but if there's still trouble, please post back here with a clear code sample and error and we'll revisit.
@roji - I shared a bunch in the original description?
I don't think any exceptions are applicable here. But there might be something here when trying to configure EF within Aspire setups.
@atrauzzi I do think we should post some Aspire-specific guidance in the docs... There's also some configuration improvements coming in 9 which could intersect with this (release notes). I'll keep it in mind... In any case if you think there's still a problem, please post back.
Will do. 💕
Would really appreciate some Aspire-specific guidance, I'm running into the same problem.