问题描述
我一直在尝试为我的集合类型创建导航属性,我发现 这个例子 一个人如何使用 OnModelCreating 完成它.我在我的 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);
}
}
这是我的多对多关系模型:
Here are my to models with the many to many relationship:
更新以显示 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
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 });
另见:
这篇关于在使用 OnModelCreating 和 ApplicationDbContext 时解决“无键定义"错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!