基于示例MVC5项目,我试图学习处理迁移的正确方法。获取多个DbContexts以使用迁移相同的数据库
在根目录中我有一个名为“DbContexts”与两个Contextes的文件夹。
第一招: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" }
// );
//
}
}
然后,我有类似的性质MyContexts。
当我尝试运行“更新-数据库”命令,我收到此错误信息: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中吗?我见过的每个示例都将ASP.NET Identity保留为默认值,然后创建自己的调用相同数据库的DbContext,并且遇到问题,告诉Migrations同时接受这两个DbContext。 – Kaleet
是的,它应该没问题。他们只是给你一个默认的DbContext开箱 - 你可以根据需要修改它,但你不需要创建第二个。我已经看过一些关于使用独立IdentityContext和其他上下文的文章,但对我来说,它只是过于复杂,除非你正在处理某些场景。 – Jack