[英]Unable to create an object of type 'ApplicationDbContext'. Add an implementation of 'IDesignTimeDbContextFactory<ApplicationDbContext>' to the project
[英]Error "Unable to create an object of type 'ApplicationDbContext'."
我正在使用 Npgsql 和實體框架。 我正在嘗試進行新的遷移,但問題標題出現錯誤。 我已經嘗試過MS 文檔中的示例。
所以,我的代碼是:
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);
}
}
您可能需要查看構建您自己的 DbContext 類的文檔和 DbContext 創建的文檔,以了解一些其他注意事項/最佳實踐。
我注意到的第一件事是您的上下文類不包含無參數構造函數或接受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);
}
}
我已經解決了這個問題! 所以,我只有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
- 有效!
PS 不要忘記appsettings.<Your ASPNETCORE_ENVIRONMENT>.json
文件。
我有同樣的問題,我的項目中的問題是我將數據層作為啟動項目。 所以我把它改成StartUp類所在的項目就夠了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.