我正在使用数据库优先方法在我的项目中使用MVC Identity,我有方法将默认身份覆盖到我的自定义创建身份。仅当我连接到web.config中的localDB时才有效连接字符串如下所示,其命中覆盖方法 OnModelCreating ,但如果我使用服务器则不会使用该功能,因为我得到了异常的建议。
//If i use this connection string it works//
<add name="TestEntities" connectionString="Data Source=kittyrythmDB;database=TestDB;integrated security=true;" providerName="System.Data.SqlClient" />
public class ApplicationDbContext : IdentityDbContext<MyUser, MyRole, long, MyLogin, MyUserRole, MyClaim>
{
public ApplicationDbContext()
: base("TestEntities")
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Map Entities to their tables.
modelBuilder.Entity<MyUser>().ToTable("User");
modelBuilder.Entity<MyRole>().ToTable("Role");
modelBuilder.Entity<MyClaim>().ToTable("UserClaim");
modelBuilder.Entity<MyLogin>().ToTable("UserLogin");
modelBuilder.Entity<MyUserRole>().ToTable("UserRole");
// Set AutoIncrement-Properties
modelBuilder.Entity<MyUser>().Property(r => r.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<MyClaim>().Property(r => r.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<MyRole>().Property(r => r.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
// Override some column mappings that do not match our default
modelBuilder.Entity<MyUser>().Property(r => r.UserName).HasColumnName("Login");
modelBuilder.Entity<MyUser>().Property(r => r.PasswordHash).HasColumnName("Password");
}
}
//But if i use this connection string its not working
<add name="TestEntities" connectionString="metadata=res://*/DAL.TestDB.csdl|res://*/DAL.TestDB.ssdl|res://*/DAL.TestDB.msl;provider=System.Data.SqlClient;provider connection string="data source=xxxxx;initial catalog=TestDB;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />