npgsql / efcore.pg

Entity Framework Core provider for PostgreSQL
PostgreSQL License
1.56k stars 225 forks source link

EF9.0 UseNpgsql MigrationsAssembly setting is invalid #3365

Open dashiell-zhang opened 23 hours ago

dashiell-zhang commented 23 hours ago
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Npgsql;
using Repository.Tool.Tasks;

namespace Repository.Tool
{
    internal class Program
    {
        static void Main(string[] args)
        {
            IHost host = Host.CreateDefaultBuilder(args)
                .ConfigureServices((hostContext, services) =>
                    {
                        services.AddDbContext<Database.DatabaseContext>(options =>
                        {
                            var connectionString = "Host=127.0.0.1;Database=webcore;Username=postgres;Password=123456";

                            NpgsqlDataSourceBuilder dataSourceBuilder = new(connectionString);

                            options.UseNpgsql(dataSourceBuilder.Build(), x => x.MigrationsAssembly("Repository.Tool"));
                        });
                        services.AddHostedService<SyncJsonIndexTask>();
                    }).Build();

            host.Run();
        }
    }
}

My DatabaseContext is placed in the Repository class library. I created a Repository.Tool project specifically to perform EF migration operations. At the same time, I set the Migrations folder in the migration process in Repository.Tool. I have always done this in .net 6.0 - 8.0. Today, after upgrading to .net 9.0 and pairing it with Npgsql.EntityFrameworkCore.PostgreSQL 9.0.0-rc.2, I found that the following problem occurred when performing EF migration. I withdrew .net 8.0 and did the same operation.

PM> Add-Migration Init Build started... Build succeeded. No DbContext was found in assembly 'Repository.Tool'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic. PM>

Sample project https://github.com/dashiell-zhang/NetEngine.git

image

dashiell-zhang commented 23 hours ago
        options.UseNpgsql(dataSourceBuilder.Build());

I found that even if I remove the MigrationsAssembly setting, the same error will occur.

dashiell-zhang commented 22 hours ago

Remaining arguments: . Finding DbContext classes... Finding IDesignTimeDbContextFactory implementations... Finding DbContext classes in the project...

The main problem is that the DatabaseContext in the Repository in the Repository.Tool dependent project cannot be found in EF 9.0, but it can be found in EF 8.0.

dashiell-zhang commented 22 hours ago

After switching the database driver to Microsoft.EntityFrameworkCore.SqlServer and using Microsoft.EntityFrameworkCore.Relational 9.0.0 official version and Microsoft.EntityFrameworkCore.Tools 9.0.0 official version, the problem no longer exists. Migration files can be generated normally. Therefore, it is inferred that the problem should be caused by Npgsql.EntityFrameworkCore.PostgreSQL. I am using

Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.0-rc.2"
Include="Microsoft.EntityFrameworkCore.Relational" Version="9.0.0-rc.2.24474.1"
Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.0-rc.2.24474.1"
roji commented 20 hours ago

Originally opened in EF as https://github.com/dotnet/efcore/issues/35103