C# 支持';应用程序上下文';自创建数据库以来,上下文已更改。考虑使用代码第一迁移

C# 支持';应用程序上下文';自创建数据库以来,上下文已更改。考虑使用代码第一迁移,c#,asp.net-mvc,entity-framework,asp.net-mvc-5,code-first,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> { } p

我定制了
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");
    }
公共类ApplicationUserRole:IdentityUserRole
{
}
公共类ApplicationUserClaim:IdentityUserClaim
{
}
公共类ApplicationUserLogin:IdentityUserLogin
{
}
公共类应用程序角色:IdentityRole
{
公共应用程序角色(){}
公共应用程序角色(字符串名称){name=name;}
}
公共类ApplicationUserStore:UserStore
{
公共ApplicationUserStore(ApplicationDbContext上下文)
:基本(上下文)
{
}
}
公共类应用程序RoleStore:RoleStore
{
公共应用程序角色存储(ApplicationDbContext上下文)
:基本(上下文)
{
}
}
公共类应用程序用户:IdentityUser
{
公共异步任务GenerateUserIdentityAsync(ApplicationUserManager)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}
}
公共类ApplicationDbContext:IdentityDbContext
{
公共应用程序上下文()
:base(“默认连接”)
{
}
公共静态应用程序上下文创建()
{
返回新的ApplicationDbContext();
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
基于模型创建(modelBuilder);
modelBuilder.Entity().HasKey(l=>l.Id).ToTable(“tbl_用户”、“成员资格”);
modelBuilder.Entity().HasKey(l=>l.Id).ToTable(“tbl_角色”、“成员资格”);
modelBuilder.Entity().HasKey(l=>l.Id).ToTable(“tbl_用户声明”、“成员资格”);
modelBuilder.Entity().HasKey(l=>new{l.UserId,l.ProviderKey,l.LoginProvider}).ToTable(“tbl_UserLogins”,“membership”);
modelBuilder.Entity().HasKey(l=>new{l.RoleId,l.UserId}).ToTable(“tbl_UserRoles”,“membership”);
}
当我第一次运行它时,一切正常,在SQL Server中创建表;但如果我在类
ApplicationUser
中添加属性(例如
public string FirstName{get;set;}
),并运行它在数据库中进行更改,则抛出错误:

支持“AptudioDbValue'上下文的模型自创建数据库以来发生了变化。请考虑使用代码第一迁移来更新数据库


我知道我必须启用迁移,但有没有办法不用迁移就可以实现迁移,因为它会创建自己的文件夹(配置)并在其中生成类?

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

protected void Application_Start()
{
  //Other calls...
  Database.SetInitializer(new DropCreateDatabaseIfModelChanges<ApplicationDbContext>());
}
protected void Application_Start()
{
  //Other calls...
  Database.SetInitializer(new ApplicationDbInitializer());
} 
然后在Global.asax中注册:

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

我做到了,但现在抛出此错误。无法删除数据库“TestSecurity”因为它当前正在使用。@www1986很可能您正在从Management Studio或Visual Studio Server Explorer访问此数据库。请确保没有打开到此数据库的连接,或者无法删除正在使用的内容。关闭用于查询此数据库的任何应用程序。如果不起作用,请暂停并重试启动SQL Server实例,这将关闭所有左侧连接。您如何可能建议
DropCreateDatabaseIfModelChanges
解决此问题?您是否意识到有人阅读您的答案并将其放入生产代码中的变化很大?@Mihai andridiclescu我的答案符合问题,请阅读所有内容因此,请这样做:“[但有没有办法不进行迁移]”@Fals是的,它可以工作。但现在我有另一个问题,正如您在上面的数据库中所写。SetInitializer(new DropCreateDatabaseIfModelChanges())这会删除数据库,然后创建,所有旧信息都会丢失。如何在不丢失旧记录的情况下执行此操作?