基于示例 MVC5 项目,我正在尝试学习处理迁移的正确方法。
在根目录中,我有一个名为“DbContexts”的文件夹,其中包含两个上下文。
第一个:IdentityContext.cs
public class IdentityContext : IdentityDbContext<ApplicationUser>
{
public IdentityContext()
: base("DefaultConnection")
{ }
}
然后我有一个名为 IdentityMigrations 的文件夹,其中包含 Configuration.cs
internal sealed class Configuration : DbMigrationsConfiguration<TryFive.Web.DbContexts.IdentityContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
MigrationsDirectory = @"DbContexts\IdentityMigrations";
}
protected override void Seed(TryFive.Web.DbContexts.IdentityContext 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. E.g.
//
// context.People.AddOrUpdate(
// p => p.FullName,
// new Person { FullName = "Andrew Peters" },
// new Person { FullName = "Brice Lambson" },
// new Person { FullName = "Rowan Miller" }
// );
//
}
}
然后我有具有类似属性的 MyContext。
当我尝试运行“更新数据库”命令时,我收到此错误消息:
The type 'TryFive.Web.DbContexts.IdentityContext' does not inherit from 'System.Data.Entity.Migrations.DbMigrationsConfiguration'. Migrations configuration types must extend from 'System.Data.Entity.Migrations.DbMigrationsConfiguration'.
关于如何解决此问题或执行此 DbContext 操作的更好方法有什么想法吗?
建议:如果您只是像您所说的那样做一个示例项目来学习迁移,请坚持使用一个 DbContext。保持简单 - 将您的实体合并到一个继承自
IdentityDbContext<ApplicationUser>
的 DbContext 中
删除您创建的现有迁移文件夹 - 并在删除第二个 DbContext 后重新“启用迁移”。这将帮助您继续学习迁移,而不是学习如何在一个项目中使用两个 DbContext。
另外,@Lajos,我不确定你在谈论哪个 MVC,但我的 DbContext 从未从 DbMigrationsConfiguration 继承 - 它们从 DbContext 或 IdentityDbContext 继承。您所指的是在项目上发出“enable-migrations”时生成的 MigrationsConfiguration 类。它用于生成迁移和播种数据。