Vit*_*liy 9 asp.net-core-2.1 entity-framework-migrations
我有 ASP.NET Core 1.1 项目,我正在尝试将其转换为 v2.1。我找到了IDesignTimeDbContextFactory实现,但我不明白我应该在哪里创建这个工厂的实例以及如何将它用于迁移正常工作。现在在创建/删除迁移之前,我必须清理并重建解决方案,因为如果没有,我会收到以下消息:Unable to create an object of type 'MyDbContext'. Add an implementation of 'IDesignTimeDbContextFactory<MyDbContext>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time。所有上下文数据都在单独的项目中。如何使用IDesignTimeDbContextFactory实现或我做错了什么?
Ara*_*ayn 11
将此类添加到您拥有 DbContext 的文件夹中。
public class MyDbContextFactory : IDesignTimeDbContextFactory<MyDbContext>
{
MyDbContext IDesignTimeDbContextFactory<MyDbContext>.CreateDbContext(string[] args)
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
var builder = new DbContextOptionsBuilder<MyDbContext>();
var connectionString = configuration.GetConnectionString("DefaultConnection");
builder.UseSqlServer(connectionString);
return new MyDbContext(builder.Options);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9656 次 |
| 最近记录: |