无法检索元数据。在模型生成期间检测到一个或多个验证错误。 Entitytype没有定义键

时间:2015-08-27 13:14:34

标签: c# entity-framework ef-code-first

我正在为我的项目使用EF代码第一种方法。我有一个'订单'具有外键的模型' UserID' ' ApplicationUser'模型如下图所示:

public class Order
{
    [Key]
    public int OrderID { get; set; }

    public virtual string UserID { get; set; }
    public decimal TotalPrice { get; set; }

    public bool OrderShipped { get; set; }
    public DateTime OrderDateTime { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Town { get; set; }
    public string Country{ get; set; }
    public string PostalCode { get; set; }

    [ForeignKey("UserID")]
    public virtual ApplicationUser User { get; set; }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}

问题在于每当我尝试使用Model类&#39; Order.cs(MajorProject2.Models)&#39;添加新控制器时。和数据上下文类&#39; MajorProject2Context(MajorProject2.Models)&#39;,它给出了如下错误:

  

运行所选代码生成器时出错:&#39;无法检索&#39; MajorProject2.Models.Order&#39;。

     

在模型生成期间检测到一个或多个验证错误:

     

IdentityUserLogin :: EntityType&#39; IdentityUserLogin&#39;没有定义键。   为此EntityType定义。

     

IdentityUserRole :: EntityType&#39; IdentityUserRole&#39;没有定义键。   为此EntityType定义。

     

IdentityUserLogins:EntityType:EntitySet&#39; IdentityUserLogins&#39;基于类型&#39; IdentityUserLogin&#39;没有定义键。

     

IdentityUserRoles:EntityType:EntitySet&#39; IdentityUserRoles&#39;基于类型&#39; IdentityUserRole&#39;没有定义键。

如何解决这个错误?我从相关问题尝试了不同的解决方案,但没有一个能够奏效。

2 个答案:

答案 0 :(得分:0)

我有两个DbContext:MajorProject2Context和ApplicationDbContext。我得到了这个错误,因为我在MajorProjectContext的一个模型中有ApplicationDbContext的外键。我找到的唯一解决方案是将两个DbContexts合并为一个DbContext(ApplicationDbContext)。我不得不做一些改变,但现在一切正常。

答案 1 :(得分:0)

您只是忘了在班级订单中添加ID。你看,EF用你的filds生成表。它需要名为ID的字段。 所以将private let commandKey = NSEventModifierFlags.command.rawValue private let commandShiftKey = NSEventModifierFlags.command.rawValue | NSEventModifierFlags.shift.rawValue override func performKeyEquivalent(with event: NSEvent) -> Bool { if event.type == NSEventType.keyDown { if (event.modifierFlags.rawValue & NSEventModifierFlags.deviceIndependentFlagsMask.rawValue) == commandKey { switch event.charactersIgnoringModifiers! { case "x": if NSApp.sendAction(#selector(NSText.cut(_:)), to:nil, from:self) { return true } case "c": if NSApp.sendAction(#selector(NSText.copy(_:)), to:nil, from:self) { return true } case "v": if NSApp.sendAction(#selector(NSText.paste(_:)), to:nil, from:self) { return true } case "z": if NSApp.sendAction(Selector(("undo:")), to:nil, from:self) { return true } case "a": if NSApp.sendAction(#selector(NSResponder.selectAll(_:)), to:nil, from:self) { return true } default: break } } else if (event.modifierFlags.rawValue & NSEventModifierFlags.deviceIndependentFlagsMask.rawValue) == commandShiftKey { if event.charactersIgnoringModifiers == "Z" { if NSApp.sendAction(Selector(("redo:")), to:nil, from:self) { return true } } } } return super.performKeyEquivalent(with: event) } 更改为OrderID,一切都必须正常。