在将 OnModelCreating 与 ApplicationDbContext 一起使用时解决 'No key Defined' 错误?
Resolving 'No key Defined' errors while using OnModelCreating with ApplicationDbContext?
我一直在尝试为我的集合类型创建导航属性,我发现 一个人如何使用 OnModelCreating 完成它。我在我的 MVC 5 应用程序中试了一下,在尝试更新我的数据库时收到此错误:
One or more validation errors were detected during model generation:
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.
如何解决这些 'No key Defined' 错误?
我做了一些搜索,发现 一个用户的问题,但他的需求与我的不同。这些解决方案对于我正在尝试做的事情来说似乎相当复杂。
这是导致问题的代码:
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:
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 });
另请参阅:
- Map tables using fluent api in asp.net MVC5 EF6?
- User in Entity type MVC5 EF6
我知道这是一个迟到的回复,这可能只适用于完成教程或学校项目的人!
对我来说,只需删除数据库、.mdf 和迁移文件夹即可修复它。现在,我不知道你是否在使用迁移,但这对我有用。
我是新手,但我注意到 Identity 对密钥非常挑剔。删除迁移帮助解决了我在项目中遇到的每个(godforsaken)迁移问题。
我认为问题是您从 OnModelCreating
中删除了 base
class 的调用。尝试添加如下所示
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//Your configuration goes here
}
我一直在尝试为我的集合类型创建导航属性,我发现
One or more validation errors were detected during model generation:
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.
如何解决这些 'No key Defined' 错误?
我做了一些搜索,发现
这是导致问题的代码:
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:
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 });
另请参阅:
- Map tables using fluent api in asp.net MVC5 EF6?
- User in Entity type MVC5 EF6
我知道这是一个迟到的回复,这可能只适用于完成教程或学校项目的人!
对我来说,只需删除数据库、.mdf 和迁移文件夹即可修复它。现在,我不知道你是否在使用迁移,但这对我有用。
我是新手,但我注意到 Identity 对密钥非常挑剔。删除迁移帮助解决了我在项目中遇到的每个(godforsaken)迁移问题。
我认为问题是您从 OnModelCreating
中删除了 base
class 的调用。尝试添加如下所示
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//Your configuration goes here
}