使用实体框架命令时(" 7.0.0-beta1")。
运行时
迁移时添加InitialCreate
我收到错误。
[解决]
我尝试将我的类文件(创建DbContext的位置)从单独的类库移动到主项目,并且所有内容都按预期工作。
所以真正的问题是在单独的类库中使用DbContext。
我的dbcontext文件
public class DbTables : DbContext
{
public DbSet<class_name> class_name_alias { get; set; }
private static bool _created = false;
public DbTables()
{
if (_created)
{
Database.AsRelational().ApplyMigrations();
_created = true;
}
}
protected override void OnConfiguring(DbContextOptions options)
{
options.UseSqlServer(@"Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=app_db;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False");
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
答案 0 :(得分:0)
如果在类库中创建DBContext,要创建迁移,则必须在此类库的project.json中声明ef命令。并运行该项目的k ef命令。
{
"version": "1.0.0-*",
"dependencies": {
"EntityFramework.SqlServer": "7.0.0-beta1",
"EntityFramework.Commands": "7.0.0-beta1"
},
"commands": {
"ef": "EntityFramework.Commands"
},
"frameworks" : {
"aspnet50" : {
"dependencies": {
}
},
"aspnetcore50" : {
"dependencies": {
"System.Runtime": "4.0.20-beta-22231"
}
}
}
}
您必须覆盖on OnConfiguring方法以设置连接字符串
protected override void OnConfiguring(DbContextOptions options)
{
options.UseSqlServer(@"Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=app_db;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False");
}