自创建数据库以来,支持“ApplicationDbContext”上下文的模型已更改.请考虑使用代码优先迁移
本文关键字:迁移 代码 模型 数据库 创建 支持 上下文 ApplicationDbContext | 更新日期: 2023-09-27 18:34:14
我自定义了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中创建表;但是如果我在类ApplicationUser
中添加属性(示例public string FirstName {get; set;}
)并运行它以在数据库中进行更改,则会抛出错误:
自创建数据库以来,支持"ApplicationDbContext"上下文的模型已更改。考虑使用代码优先迁移来更新数据库
我知道我必须启用迁移,但是有没有办法在没有迁移的情况下做到这一点,因为它会创建自己的文件夹(配置)并在其中生成类?
只需在 Global.asax 上注册此条目:
protected void Application_Start()
{
//Other calls...
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<ApplicationDbContext>());
}
这将在每次其模型更改时重新创建您的身份数据库。来自标识的上下文使用与使用 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());
}