efcore.pg icon indicating copy to clipboard operation
efcore.pg copied to clipboard

Unable to configure separate migrations assembly when setting up Aspire

Open atrauzzi opened this issue 1 year ago • 3 comments

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:

atrauzzi avatar Jun 15 '24 14:06 atrauzzi

Is it okay to call the UseNpgsql overload without passing connection information? Or will this be overridden/ignored somehow outside/after my lambda?

atrauzzi avatar Jun 15 '24 14:06 atrauzzi

It does get ignored, it didn't help me with my own same situation. Can't get ef migrations add to work.

AgentFire avatar Jul 02 '24 21:07 AgentFire

This worked like a charm for me:

builder.AddNpgsqlDbContext<EmployeesDbContext>(
    "employees-db",
    configureDbContextOptions: options => options.UseNpgsql(b => b.MigrationsAssembly(typeof(Program).Assembly.FullName)));

vladislav-karamfilov avatar Sep 02 '24 08:09 vladislav-karamfilov

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 avatar Oct 29 '24 15:10 roji

@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 avatar Oct 29 '24 16:10 atrauzzi

@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.

roji avatar Oct 29 '24 18:10 roji

Will do. 💕

atrauzzi avatar Oct 29 '24 18:10 atrauzzi

Would really appreciate some Aspire-specific guidance, I'm running into the same problem.

rsalus avatar Nov 20 '24 03:11 rsalus