我收到以下错误消息:
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()); } }
解决方法
在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的工作正在进行中,这将被修复(?)
原文地址:https://www.jb51.cc/aspnet/250466.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。