本文介绍了使用ApplicationDbContext使用OnModelCreating解析“无键定义”错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我一直在为我的收藏类型创建导航属性,我发现的实例框架。我试图在我的MVC 5应用程序中尝试更新我的数据库时收到此错误:I've been trying to create navigation properties for my collection types and I found this example of how one person accomplished it using OnModelCreating. I gave it a try in my MVC 5 application and I recieved this error when trying to update my database: BlogEngine.Models.IdentityUserLogin::EntityType'IdentityUserLogin'没有定义键。定义此EntityType的键。 BlogEngine.Models.IdentityUserRole::EntityType'IdentityUserRole'没有定义键。定义此EntityType的键。 IdentityUserLogins:EntityType:EntitySet'IdentityUserLogins'是基于没有定义键的IdentityUserLogin类型的。 IdentityUserRoles:EntityType:EntitySet'IdentityUserRoles'基于类型'IdentityUserRole',没有定义键。BlogEngine.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. BlogEngine.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. 如何解决这些'没有键定义'错误? 我做了一些搜索,我发现这个解决方案为一个用户的问题,但他的需求是不同于我的。解决方案似乎是令人讨厌的,我正在尝试做的。How do I resolve these 'No key Defined' errors?I did some searching and I found this solution for one users problem, but his needs were different than mine. The solutions seemed pretty convoluted for what I am trying to do.这是导致问题的代码:public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false) { } //public DbSet<Image> Images { get; set; } public static ApplicationDbContext Create() { return new ApplicationDbContext(); } public DbSet<Post> Posts { get; set; } public DbSet<Image> Images { get; set; } public DbSet<Album> Albums { get; set; } public DbSet<Tag> Tags { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<Post>(). HasOptional(e => e.Tags). WithMany(). HasForeignKey(m => m.Tags_Id); modelBuilder.Entity<Tag>(). HasOptional(e => e.Posts). WithMany(). HasForeignKey(m => m.Posts_Id); } }许多关系: Post.cs模型on Gist Tag.cs Gist上的模型更新以显示IdentityUser:Update to show IdentityUser:public class ApplicationUser : IdentityUser { 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; } }推荐答案你可能需要添加以下内容到 OnModelCreatingYou might have to add something like the following to OnModelCreatingmodelBuilder.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 }); 另请参阅: 地图在asp.net MVC5 EF6中使用流畅api的表 实体类型中的用户MVC5 EF6Map tables using fluent api in asp.net MVC5 EF6?User in Entity type MVC5 EF6 这篇关于使用ApplicationDbContext使用OnModelCreating解析“无键定义”错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-17 06:56