使用vs2019创建项目迁移数据库时EFCore 报错

1.执行Add-Migration Migrations 报一下错误

Unable to create an object of type 'MyDBContent'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

2.解决方法

通过在尝试在MyDBContent同级目录下添加类DesignTimeDbContextFactory 成功解决

public class DesignTimeDbContextFactory: IDesignTimeDbContextFactory<MyDBContent>
{
public IConfiguration Configuration { get; }
public MyDBContent CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<MyDBContent>();
var sqlConnection = Configuration.GetConnectionString("SqlServerConnection");
optionsBuilder.UseSqlServer(sqlConnection);

return new MyDBContent(optionsBuilder.Options);
}
}

posted @ 2020-05-24 15:42  孜孜不倦超越自我  阅读(300)  评论(0)    收藏  举报