【发布时间】:2019-05-01 10:48:18
【问题描述】:
我有 2 个模型类:
- Customer.cs 带有名称和 ID
- Movies.cs 带有名称和 ID
我尝试运行enable-migrations,但出现此错误:
在程序集 WebApplication2' 中找不到上下文类型。
然后我在网站上看到了一些答案,人们告诉我要开设DBContext 课程。我没有任何 DBContext 类,因为我刚刚创建了一个新的 MVC 项目。
因此,我尝试创建自己的 DbContext 类,如下所示:
{
public class MyDBContext:DbContext
{
public void MyDbContext()
{
}
}
}
然后我能够运行enable-migrtaions 命令并使用 configuration.cs 创建迁移文件夹,如下所示:
internal sealed class Configuration : DbMigrationsConfiguration<WebApplication2.Models.MyDBContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(WebApplication2.Models.MyDBContext context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data.
}
}
}
现在,当我运行 add-migration Initialmodel 时,Up() 和 Down() 方法为空,并且没有身份表。 请帮忙!
【问题讨论】:
-
急需帮助
-
您不需要在上下文中添加 DbSet 吗?
标签: asp.net asp.net-mvc entity-framework ef-code-first entity-framework-migrations