EF模型验证 - 没有定义键

时间:2017-07-11 14:16:39

标签: asp.net-mvc entity-framework

我之前从未遇到过这个错误:

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.

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

这是我的背景:

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); 
        } 
    }
}

任何人都可以指出我正确的方向,如何解决?

1 个答案:

答案 0 :(得分:1)

您好我很久以前也遇到了同样的错误,在 OnModelCreating 方法中放置了一行 base.OnModelCreating(modelBuilder); ,解决了我的问题。

您更改的代码:

OpenCV_2.4.11_Manager_2.20_armv7a-neon-android8.apk

有用的链接:https://stackoverflow.com/a/34786782/3397630No key Defined' errors

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

由于

KARTHIK