EntityType'IdentityUserLogin'沒有定義鍵。定義此EntityType的鍵我正在使用Entity Framework Code First和MVC 5.當我使用個人用戶帳戶身份驗證創(chuàng)建我的應用程序時,我獲得了一個帳戶控制器以及所有必需的類和代碼,以使Indiv用戶帳戶身份驗證工作。已經(jīng)存在的代碼包括:public class ApplicationDbContext : IdentityDbContext<ApplicationUser>{
public ApplicationDbContext() : base("DXContext", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}}但后來我繼續(xù)使用代碼創(chuàng)建了我自己的上下文,所以我現(xiàn)在也有以下內(nèi)容:public class DXContext : DbContext{
public DXContext() : base("DXContext")
{
}
public DbSet<ApplicationUser> Users { get; set; }
public DbSet<IdentityRole> Roles { get; set; }
public DbSet<Artist> Artists { get; set; }
public DbSet<Paintings> Paintings { get; set; } }最后,我有以下種子方法為我添加一些數(shù)據(jù),同時開發(fā):protected override void Seed(DXContext context){
try
{
if (!context.Roles.Any(r => r.Name == "Admin"))
{
var store = new RoleStore<IdentityRole>(context);
var manager = new RoleManager<IdentityRole>(store);
var role = new IdentityRole { Name = "Admin" };
manager.Create(role);
}
context.SaveChanges();
if (!context.Users.Any(u => u.UserName == "James"))
{
var store = new UserStore<ApplicationUser>(context);
var manager = new UserManager<ApplicationUser>(store);
var user = new ApplicationUser { UserName = "James" };
manager.Create(user, "ChangeAsap1@");
manager.AddToRole(user.Id, "Admin");
}我的解決方案構建正常,但當我嘗試訪問需要訪問數(shù)據(jù)庫的控制器時,我收到以下錯誤:DX.DOMAIN.Context.IdentityUserLogin :: EntityType'IdentityUserLogin'沒有定義鍵。定義此EntityType的鍵。DX.DOMAIN.Context.IdentityUserRole :: EntityType'IdentityUserRole'沒有定義鍵。定義此EntityType的鍵。我究竟做錯了什么?是因為我有兩個背景嗎
3 回答

慕的地10843
TA貢獻1785條經(jīng)驗 獲得超8個贊
在我的情況下,我正確地從IdentityDbContext繼承(使用我自己的自定義類型和鍵定義),但無意中刪除了對基類的OnModelCreating的調(diào)用:
protected override void OnModelCreating(DbModelBuilder modelBuilder){ base.OnModelCreating(modelBuilder); // I had removed this /// Rest of on model creating here.}
然后,我從標識類中修復了丟失的索引,然后我可以生成遷移并適當?shù)貑⒂眠w移。
- 3 回答
- 0 關注
- 1511 瀏覽
添加回答
舉報
0/150
提交
取消