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
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 });
...
In my case, I already have a database (Database firts). Thanks to all comments here, I found my solution:
The tables must have the relationship but the name of the columns need to be different and add ForeignKey attribute.
[ForeignKey("PrestadorId")] public virtual AwmPrestadoresServicios Colaboradores { get; set; }
That is, PRE_ID is PK, but FK in the other table is PRESTADOR_ID, then it works. Thanks to all comments here I found my solution. EF works in mysterious ways.
Check to see if you have any ICollections.
What I have figured out is when you have an ICollection that references a table and there is no column that it can figure out, it creates one for you to try to make the connection between the tables. This specifically happens with ICollection and has driven me "batty" trying to figure it out.