我有一个现有的DB上下文,到目前为止,EF配置效果很好,如下所示
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Configure Code First to ignore PluralizingTableName convention
// If you keep this convention then the generated tables will have pluralized names.
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<StoreGeneratedIdentityKeyConvention>();
}
我跟着MSDN article扩展了MVC提供的Identity模型,以链接到我现有数据库上下文中的某个人。
App用户
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> 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 virtual Person LinkedPerson { get; set; }//My reference to existing person definition
}
上下文
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
public DbSet<Person> People { get; set; }
}
只要我不使用注册或登录功能,该应用就可以正常工作。然而,一旦我注册为用户,我的所有模型现在都是重复的,一个是单数,另一个是复数形式。
在哪里可以告诉MVC身份框架不要重新创建多个表并使用现有的(Singular)数据库对象?
答案 0 :(得分:3)
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
对身份表不起作用。所以需要明确解决:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Configure Code First to ignore PluralizingTableName convention
// If you keep this convention then the generated tables will have pluralized names.
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<StoreGeneratedIdentityKeyConvention>();
modelBuilder.Entity<IdentityRole>().ToTable("AspNetRole");
modelBuilder.Entity<IdentityUserRole>().ToTable("AspNetUserRole");
modelBuilder.Entity<IdentityUserLogin>().ToTable("AspNetUserLogin");
modelBuilder.Entity<IdentityUserClaim>().ToTable("AspNetUserClaim");
modelBuilder.Entity<ApplicationUser>().ToTable("AspNetUser");
}