EF 模型验证 - 未定义密钥



我以前从未遇到过此错误:

Server Error in '/' Application.
One or more validation errors were detected during model generation:
wc2018.DAL.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
wc2018.DAL.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.

我以前从未遇到过身份/角色/用户/登录的任何问题,但是当我尝试通过我的 Web 应用程序注册新用户时,这就是我得到的。

这是我的背景:

using Microsoft.AspNet.Identity.EntityFramework;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using wc2018.Models;
namespace wc2018.DAL
{
public class WC2018Context : IdentityDbContext<ApplicationUser>
{
public WC2018Context() : base("WC2018Context", throwIfV1Schema: false)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
public static WC2018Context Create()
{
return new WC2018Context();
}
public System.Data.Entity.DbSet<wc2018.Models.Team> Teams { get; set; }
}
}

播种机:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using wc2018.Models;
using wc2018.DAL;
namespace wc2018.Models
{
public class WC2018Initialiser : DropCreateDatabaseAlways<WC2018Context> { 
protected override void Seed(WC2018Context context) { 
context.Teams.Add(new Team { Name = "David's Mob", Owner = "DS" });
context.Teams.Add(new Team { Name = "Christo's Minions", Owner = "CJ The Winner" }); 
context.SaveChanges(); 
base.Seed(context); 
} 
}
}

谁能指出我正确的方向,关于如何解决?

嗨,我很久以前也有同样的错误,通过放置这个一行基础。OnModelCreateating(modelBuilder(;OnModel创建方法中,解决了我的问题。

您更改的代码:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}

有用的链接:https://stackoverflow.com/a/34786782/3397630,未定义键的错误

希望它也能解决您的问题。

谢谢

卡尔西克

最新更新