Entity Framework - Invalid Column Name '*_ID"

后端 未结 15 1630
长发绾君心
长发绾君心 2020-11-29 21:30

I\'ve narrowed this down to some issue between Code First and Database first EF, but I\'m not sure how to fix it. I\'ll try to be as clear as I can, but I honestly am missin

15条回答
  •  情话喂你
    2020-11-29 22:09

    In my case I was incorrectly defining a primary key made up of two foreign keys like this:

    HasKey(x => x.FooId);
    HasKey(x => x.BarId);
    
    HasRequired(x => x.Foo)
        .WithMany(y => y.Foos);
    HasRequired(x => x.Bar);
    

    The error I was getting was, "invalid column name Bar_ID".

    Specifying the composite primary key correctly fixed the problem:

    HasKey(x => new { x.FooId, x.BarId });
    
    ...
    

提交回复
热议问题