10

I'm using a 'table-per-type' hierarchy in my code-first project (EF5). My derived classes override the default primary-key name to clearly identify this relationship from a database point of view, like so:

/* 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 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 on property Table.Chairs: Column 'Id' specified as part of this MSL does not exist in MetaDataWorkspace.

Which I kinda understand as EF probably didn't see that the PK of Chair Product was changed.. (and still assumes it's called 'Id') But I can't figure out how I instruct EF to use the alternate key.

Thx,

PS. Yes I know, if I don't change the names of the PK's it works... but can it be done using the EF Fluent API?

Elmar
  • 332
  • 1
  • 3
  • 11
  • 2
    Found the solution 30min after I posted. All I needed to do was add the following: modelBuilder.Entity().HasMany(x => x.Chairs).WithMany().Map(m => { m.MapLeftKey("TableId"); m.MapRightKey("ChairId"); m.ToTable("TableChairs"); }); And everything was working as expected ;)
    – Elmar Sep 04 '13 at 14:46

5 Answers5

5

This is some bug of EDMX model generator in Visual Studio.

To solve this do:

  • remove particular table from EF.
  • save EF and restart Visual Studio
  • add particular table to the model again
  • save and compile

I have Visual Studio 2015 udpate 3

Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148
  • 2
    I was about to post this question and answer until I found this. I would emphasize the restarting of Visual Studio. What a weird fix. – christo8989 Jan 23 '17 at 21:19
  • This solution seems to work for me but the problem is since I have to generate the table/model again(using dev db), it will add other stuff that i don't need(or not sure is in prod db) which could break production. – alltej Mar 29 '17 at 19:23
  • I think this is a legit bug on the EF generation or with VS. I already suspected something is not working correctly in my local machine because I added manually the column as posted here http://stackoverflow.com/questions/35950795/adding-a-new-column-to-an-existing-table-in-entity-framework/38795367#38795367 (an answer I posted a while back) by manually adding the column. To prove it, I ask my co-worker to add manually the columns in the edmx file. He checked-in code, got latest in my machine and the update works. – alltej Mar 29 '17 at 20:08
1

I recently was searching for a fix to a similar error, but designing things Database first. I post my finding here because this is where my search kept landing me.

I had multiple similar errors, on build. What they really meant is that my Model never built successfully.

I was able to fix the issue by removing and re-adding some table valued functions that had been modified in the database recently.

Most of the errors and details I could find where actually just red-herrings; other than the concept that the model didn't build successfully; and having knowledge of things someone else was changing in the database helped.

Greg
  • 2,410
  • 21
  • 26
1

I was experiencing similar errors and had made some manual fixes to my edmx file, but Visual Studio was still reporting this warning.

Turns out it was a false warning and clean/rebuild got rid of the error. Spent hours trying to figure out what was wrong with the edmx file but never thought of giving it a run and checking if it was actually a problem.

Nick Pickering
  • 3,095
  • 3
  • 29
  • 50
0

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"); 
});
Sameer Alibhai
  • 3,092
  • 4
  • 36
  • 36
0

I got the same one. Looking at all this I thought something is off with visual studio I mean no software is perfect right, a minute later I found a typo in my code :-|

Niraj Motiani
  • 71
  • 1
  • 8