错误“无法创建类型为'ApplicationDbContext'的对象。

时间:2020-01-29 15:35:04

标签: c# asp.net-core .net-core npgsql

我正在使用Npgsql和Entity Framework。我正在尝试进行新的一次迁移,但是问题标题出了错误。我已经尝试过MS documentation中的示例。

所以,我的代码是:

public class Program
{
    public static void Main(string[] args)
        => CreateHostBuilder(args).Build().Run();

    // EF Core uses this method at design time to access the DbContext
    public static IHostBuilder CreateHostBuilder(string[] args)
        => Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(
                webBuilder => webBuilder.UseStartup<Startup>());
}

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseNpgsql(Configuration.GetConnectionString("LalalaDb")));
    }
}

问题出在哪里?我如何进行迁移?

更新

这是我的ApplicationDbContext.cs

public class ApplicationDbContext : IdentityDbContext
{
    private readonly string _connectionString;

    public DbSet<Customer> Customers { get; set; }
    // and more DbSets are here ... 

    public ApplicationDbContext(IConfiguration configuration)
    {
        _connectionString = configuration.GetConnectionString("LalalaDb");
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseNpgsql(_connectionString, b => b.MigrationsAssembly("EntityFrameworkProject"));
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}

3 个答案:

答案 0 :(得分:1)

您可能需要查看documentation for building your own DbContext classdocumentation for DbContext Creation,以了解其他一些注意事项/最佳做法。

我注意到的第一件事是您的上下文类不包含无参数构造函数或采用DbContextOptions<T>实例的构造函数,这很可能是无法创建迁移的原因。

public class ApplicationDbContext : IdentityDbContext
{
    private readonly string _connectionString;

    public DbSet<Customer> Customers { get; set; }
    // and more DbSets are here ... 

    public ApplicationDbContext() : base()
    { }

    public ApplicationDbContext(IConfiguration configuration)
    {
        _connectionString = configuration.GetConnectionString("LalalaDb");
    }

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    { }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseNpgsql(_connectionString, b => b.MigrationsAssembly("EntityFrameworkProject"));
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}

答案 1 :(得分:0)

我已经解决了这个问题! 因此,我只有Class library项目。在项目中,我有:

  • ApplicationDbContext.cs(就像问题中一样)
  • 带有迁移的文件夹。

然后我将Program.cs文件放入以下内容:

public class Program
{
    public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
    {
        public ApplicationDbContext CreateDbContext(string[] args)
        {
            var envName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

            IConfigurationRoot configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile($"appsettings.{envName}.json")
                .Build();
            var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
            var connectionString = configuration.GetConnectionString("LalalalDb");
            builder.UseNpgsql(connectionString);
            return new ApplicationDbContext(configuration);
        }
    }
}

最后,

$ dotnet ef migrations add Lalalalalal-有效!

P.S。不要忘记appsettings.<Your ASPNETCORE_ENVIRONMENT>.json文件。

答案 2 :(得分:0)

我遇到了同样的问题,我的项目中的问题是我将数据层作为启动项目。所以我把它改成StartUp类所在的项目就够了。