我正在尝试将个人资料/会员信息添加到我的MVC5应用程序中并添加配置映射.
我收到以下错误消息:
my.Models.IdentityUserLogin: : EntityType ‘IdentityUserLogin’ has no
key defined. Define the key for this EntityType.my.Models.IdentityUserRole: : EntityType ‘IdentityUserRole’ has no key
defined. Define the key for this EntityType.IdentityUserLogins: EntityType: EntitySet ‘IdentityUserLogins’ is
based on type ‘IdentityUserLogin’ that has no keys defined.IdentityUserRoles: EntityType: EntitySet ‘IdentityUserRoles’ is based
on type ‘IdentityUserRole’ that has no keys defined.
public class ApplicationUser : IdentityUser { public string City { get; set; } public string Discriminator { get; set; } public string Address { get; set; } } public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new ApplicationUserConfiguration()); } }
解决方法
调用base.OnModelCreating(modelBuilder)并没有解决我的问题.
在VS2013-Preview,VS2013-RC和VS2013-RTM中,Microsoft.AspNet.Identity.EntityFramework的行为似乎不同.我正在使用RTM版本.
从IdentityUser继承之后,我不得不重新创建模型中的所有其他主键,使其工作:
public class ApplicationUser : IdentityUser { public string DisplayName { get; set; } } public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId); modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id); modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId,r.UserId }); }
(见Configuring/Mapping Properties and Types with the Fluent API)
我猜AspNet.Identity.EntityFramework的工作正在进行中,这将被修复(?)