使用ApplicationDbContext使用OnModelCreating解析“无键定义”错误? [英] Resolving 'No key Defined' errors while using OnModelCreating with ApplicationDbContext?
问题描述
我一直在为我的收藏类型创建导航属性,我发现的实例框架。我试图在我的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:
一个或多个验证错误是在模型生成期间检测到:
One or more validation errors were detected during model generation:
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.
< h2>如何解决这些'没有键定义'错误?
我做了一些搜索,我发现这个解决方案为一个用户的问题,但他的需求是不同于我的。解决方案似乎是令人讨厌的,我正在尝试做的。
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);
}
}
许多关系:
更新以显示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;
}
}
推荐答案
你可能需要添加以下内容到 OnModelCreating
You might have to add something like the following to OnModelCreating
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 });
另请参阅:
- Map tables using fluent api in asp.net MVC5 EF6?
- User in Entity type MVC5 EF6
这篇关于使用ApplicationDbContext使用OnModelCreating解析“无键定义”错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!