Resolving #39;No key Defined#39; errors while using OnModelCreating with ApplicationDbContext?(在使用 OnModelCreating 和 ApplicationDbContext 时解决“未定义键错误?)
问题描述
我一直在尝试为我的集合类型创建导航属性,我发现 this example 一个人如何使用 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:
在模型生成过程中检测到一个或多个验证错误:
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.
如何解决这些未定义键"错误?
我做了一些搜索,发现 此解决方案针对一个用户的问题,但他的需求与我的不同.对于我正在尝试做的事情,这些解决方案似乎相当复杂.
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:
Gist 上的 Post.cs 模型
Gist 上的 Tag.cs 模型
更新以显示 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 });
另见:
- 在asp.net中使用fluent api映射表MVC5 EF6?
- 实体类型MVC5 EF6中的用户
这篇关于在使用 OnModelCreating 和 ApplicationDbContext 时解决“未定义键"错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在使用 OnModelCreating 和 ApplicationDbContext 时解决“未定义键"错误?
基础教程推荐
- SSE 浮点算术是否可重现? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- rabbitmq 的 REST API 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 XML 转换为通用列表 2022-01-01