EF CodeFirst THT - The "Id" column specified as part of this MSL does not exist in MetaDataWorkspace

I use the "table-per-type" hierarchy in my first code project (EF5). My derived classes override the default primary key name to clearly identify this relationship from a database perspective, for example:

/* change primary keys to Chair.ProductId and Table.ProductId */ modelBuilder.Entity<Chair>() .Property(x => x.Id) .HasColumnName("ProductId"); modelBuilder.Entity<Table>() .Property(x => x.Id) .HasColumnName("ProductId"); 

Using the following classes as an example:

 [Table("Product")] public class Product { public int Id {get; set;} /* generic product properties */ } [Table("Chair")] public class Chair : Product { /* specific chair properties */ } [Table("Table")] public class Table : Product { public virtual ICollection<Chair> Chairs {get; set;} /* specific table properties */ } 

Which causes the following error in the Table.Chairs property: The column identifier specified as part of this MSL does not exist in MetaDataWorkspace .

Which I understand as EF probably did not see that the PK of the Chair product was changed .. (and still assumes it is called "Id"). But I cannot understand how I instruct EF to use an alternate key.

thanks,

PS. Yes, I know, if I do not change the PK names, this will work ... but can this be done using the EF Fluent API?

+7
source share
4 answers

This is some error of the EDMX model generator in Visual Studio.

To resolve this issue, follow these steps:

  • delete a specific table from EF.
  • save EF and restart Visual Studio
  • add a specific table to the model again
  • save and compile

I have Visual Studio 2015 udpate 3

+4
source

I recently looked for a fix for a similar error, but first developed a database . I publish my find here because this is the place where my searches continued to drop me off.

I had several similar errors during assembly . What they really had in mind was that my Model was never created successfully .

I managed to solve this problem by deleting and re-adding some table functions that were recently changed in the database .

Most of the mistakes and details that I could find where it really is just red herring ; besides the concept that the model was not built successfully; and knowing that someone else was modifying the database helped.

+1
source

Posting your comment as an answer:

Add the following:

 modelBuilder.Entity<Table>().HasMany(x => x.Chairs).WithMany() .Map(m => { m.MapLeftKey("TableId"); m.MapRightKey("ChairId"); m.ToTable("TableChairs"); }); 
0
source

I came across similar errors and made some manual corrections in my edmx file, but Visual Studio still reported this warning.

It turns out that this was a false warning, and cleaning / restoring eliminated the error. I spent hours trying to figure out what was wrong with the edmx file, but I never thought about running it and checking if this is really a problem.

0
source

Source: https://habr.com/ru/post/953153/


All Articles