简体   繁体   English

自创建数据库以来,支持“ ApplicationDbContext”上下文的模型已更改。 考虑使用代码优先迁移

[英]The model backing the 'ApplicationDbContext' context has changed since the database was created. Consider using Code First Migrations

I customized Microsoft.AspNet.Identity classes. 我自定义了Microsoft.AspNet.Identity类。 The code is below: 代码如下:

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");
    }

When I run it first, everything works, creating tables in SQL Server; 当我第一次运行它时,一切正常,在SQL Server中创建表。 but If I add property (example public string FirstName {get; set;} ) in class ApplicationUser and run it to make changes in database, throws error: 但是如果我在类ApplicationUser添加属性(例如public string FirstName {get; set;} )并运行它以在数据库中进行更改,则会引发错误:

The model backing the 'ApplicationDbContext' context has changed since the database was created. 自创建数据库以来,支持“ ApplicationDbContext”上下文的模型已更改。 Consider using Code First Migrations to update the database 考虑使用代码优先迁移来更新数据库

I know that I must enable migrations, but is there any way to do it without migrations, because it create own folder(Configuration) and generate class in it? 我知道我必须启用迁移,但是有什么方法可以不进行迁移,因为它会创建自己的文件夹(配置)并在其中生成类?

Just register this entry at the Global.asax: 只需在Global.asax中注册此条目:

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

This will recreate your Identity Database every time that Its model changes. 每当其模型更改时,这将重新创建您的身份数据库。 The Context from Identity uses the same concept as any database you create using EF. 来自身份的上下文使用与您使用EF创建的任何数据库相同的概念。

You can use other methods, just check here for more information and exemples: System.Data.Entity Namespace 您可以使用其他方法,只需在此处检查更多信息和示例即可: System.Data.Entity命名空间

EDIT: 编辑:

To seed you database everytime It's drops you should implement a Initializer Class like this one, and override the Seed method to put some "fake" data: 每次为数据库加种子时,都应该像这样实现一个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");
        }
    }
 }

Then register this in the Global.asax: 然后在Global.asax中注册:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 模型支持 <Database> 自创建数据库以来,上下文已更改。 考虑使用代码优先迁移来更新数据库 - The model backing the <Database> context has changed since the database was created. Consider using Code First Migrations to update the database 模型支持 <MyContext> 自创建数据库以来,上下文已更改。 考虑使用代码优先迁移来更新数据库 - The model backing the <MyContext> context has changed since the database was created. Consider using Code First Migrations to update the database 自创建数据库以来,支持“ EmployeeDetailsContext”上下文的模型已更改。 - The model backing the 'EmployeeDetailsContext' context has changed since the database was created. 自数据库创建以来,我的数据库已更改。 考虑首先要更新的代码迁移 - My database has changed since the database was created. Consider code first migrations to update 如何修复“自创建数据库以来支持上下文的模型已更改。” 首先在数据库中 - How to Fix 'The model backing the context has changed since the database was created.' in database first 自创建数据库以来,支持“ ApplicationDbContext”上下文的模型已更改 - What causes The model backing the 'ApplicationDbContext' context has changed since the database was created 支持的模型<Mydb>自数据库创建以来上下文已更改 - The model backing the <Mydb> context has changed since the database was created 自创建数据库以来,支持上下文的模型已更改 - The model backing the context has changed since the database was created 自创建数据库以来,支持“MyContext”上下文的模型已更改 - The model backing the 'MyContext' context has changed since the database was created 自从在nopcommerce中创建数据库以来,支持“ ObjectContext”上下文的模型已更改 - The model backing the 'ObjectContext' context has changed since the database was created in nopcommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM