自创建数据库以来,支持'ApplicationDbContext'上下文的模型已更改。考虑使用Code First Migrations

时间:2014-12-15 18:14:53

标签: c# asp.net-mvc entity-framework asp.net-mvc-5 code-first

我定制了Microsoft.AspNet.Identity个班级。代码如下:

public class ApplicationUserRole : IdentityUserRole<Guid>
{
}

public class ApplicationUserClaim : IdentityUserClaim<Guid>
{
}

public class ApplicationUserLogin : IdentityUserLogin<Guid>
{

}

public class ApplicationRole : IdentityRole<Guid, ApplicationUserRole>
{
    public ApplicationRole() { }
    public ApplicationRole(string name) { Name = name; }
}

public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, Guid, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public ApplicationUserStore(ApplicationDbContext context)
        : base(context)
    {
    }
}

public class ApplicationRoleStore : RoleStore<ApplicationRole, Guid, ApplicationUserRole>
{
    public ApplicationRoleStore(ApplicationDbContext context)
        : base(context)
    {
    }
}

public class ApplicationUser : IdentityUser<Guid, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<ApplicationUser>().HasKey(l => l.Id).ToTable("tbl_Users", "membership");
        modelBuilder.Entity<ApplicationRole>().HasKey(l => l.Id).ToTable("tbl_Roles", "membership");
        modelBuilder.Entity<ApplicationUserClaim>().HasKey(l => l.Id).ToTable("tbl_UserClaims", "membership");
        modelBuilder.Entity<ApplicationUserLogin>().HasKey(l => new { l.UserId, l.ProviderKey, l.LoginProvider }).ToTable("tbl_UserLogins", "membership");
        modelBuilder.Entity<ApplicationUserRole>().HasKey(l => new { l.RoleId, l.UserId }).ToTable("tbl_UserRoles", "membership");
    }

当我首先运行它时,一切正常,在SQL Server中创建表;但是如果我在类public string FirstName {get; set;}中添加属性(示例ApplicationUser)并运行它以在数据库中进行更改,则会抛出错误:

  

自创建数据库以来,支持'ApplicationDbContext'上下文的模型已更改。考虑使用Code First Migrations来更新数据库

我知道我必须启用迁移,但有没有办法在没有迁移的情况下执行迁移,因为它会创建自己的文件夹(Configuration)并在其中生成类?

1 个答案:

答案 0 :(得分:-3)

只需在Global.asax注册此条目:

protected void Application_Start()
{
  //Other calls...
  Database.SetInitializer(new DropCreateDatabaseIfModelChanges<ApplicationDbContext>());
}

每次其模型更改时,这将重新创建您的身份数据库。 Identity from Identity使用与使用EF创建的任何数据库相同的概念。

您可以使用其他方法,只需在此处查看更多信息和示例:System.Data.Entity Namespace

修改

每次播放数据库时,你应该实现像这样的Initializer Class,并覆盖Seed方法来放置一些“假的”数据:

public class ApplicationDbInitializer : DropCreateDatabaseIfModelChanges<ApplicationDbContext>
{
    protected override void Seed(ApplicationDbContext context)
    {
        var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
        var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));

        if (!RoleManager.RoleExists("admim"))
        {
            var roleresult = RoleManager.Create(new IdentityRole(Administrador));
        }

        //Create User=Admin with password=123456
        var user = new ApplicationUser();
        user.UserName = "sysadmin";
        var adminresult = UserManager.Create(user, "123456");

        //Add User Admin to Role Admin
        if (adminresult.Succeeded)
        {
            var result = UserManager.AddToRole(user.Id, "admin");
        }
    }
 }

然后在Global.asax中注册:

protected void Application_Start()
{
  //Other calls...
  Database.SetInitializer(new ApplicationDbInitializer());
}