EF CodeFirst THT - Column 'Id' specified as part of this MSL does not exist in MetaDataWorkspace

entity framework code first computed column formula
entity framework ignore missing columns
entity framework identity column
fluent api
entity framework column name mapping
ef core identity column
entity framework data annotations database first
identity in entity framework code first

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?

Data Annotations Column Attribute in EF 6 & EF Core, Learn about the [Column] attribute in EF 6 and EF Core. an entity class to configure the corresponding column name, data type and order in a database table. As per the default convention, PK columns will come first and then the rest of the  An important database features is the ability to have computed properties. If you're mapping your Code First classes to tables that contain computed columns, you don't want Entity Framework to try to update those columns. But you do want EF to return those values from the database after you've inserted or updated data.

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.

Code-First Conventions in Entity Framework 6, Learn about Entity Framework 6 code-first conventions. If the foreign key property does not exist, then EF will create an FK column in the Db table with  Code First, as its name suggests, leads us to create the POCO classes first and then the database after, but it is also possible to take an existing database and generate the Code First. The Entity Framework bridges the gap between the POCO classes and the database using a container that we call Context .

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"); 
});

Code First Data Annotations, Entity Framework Code First allows you to use your own domain classes with composite foreign keys, then you must specify the same column  Data Annotations - Column Attribute in EF 6 & EF Core. The Column attribute can be applied to one or more properties in an entity class to configure the corresponding column name, data type and order in a database table. The Column attribute overrides the default convention.

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.

Tutorial: Change the database for EF Database First with ASP.NET , Using MVC, Entity Framework, and ASP.NET Scaffolding, you The generated code corresponds to the columns in the database table. In the Update Wizard, select the Refresh tab and then select Tables > dbo > Student. Update: In EF Core 2.1, for the initial migration at least, columns are added to tables in the order in which the relevant properties are declared in their respective classes, rather than in alphabetical order.

Calculated Columns in Entity Framework Code First Migrations , Calculated Columns in Entity Framework Code First Migrations While there is no native support for computed columns in Code First Migrations, we can manually configure our WriteLine("After saving: " + userProfile. Entity Framework does not do any validation of maximum length before passing data to the provider. It is up to the provider or data store to validate if appropriate. For example, when targeting SQL Server, exceeding the maximum length will result in an exception as the data type of the underlying column will not allow excess data to be stored.

Update an Existing Database using Code First Migrations with ASP , NET and Entity Framework and categories tables already exist in the database, so we need to create an initial blank migration and then later  Data Annotations - Required Attribute in EF 6 & EF Core. The Required attribute can be applied to one or more properties in an entity class. EF will create a NOT NULL column in a database table for a property on which the Required attribute is applied.

Code First Migration - Entity Framework, Then I issue the following command in package manager console. Update-​Database -Verbose -Force. Here is what my connection string to  In my Entity-Framework Code-First Model I have a Table that needs one column to have unique values. I want to apply an Unique Index to it as I saw that it is very easy in EF 6.1. public partial class User { public int Id { get; set; } [Index(IsUnique = true)] public virtual SharePoint SharePoint { get; set; } }

Comments
  • Found the solution 30min after I posted. All I needed to do was add the following: modelBuilder.Entity<Table>().HasMany(x => x.Chairs).WithMany().Map(m => { m.MapLeftKey("TableId"); m.MapRightKey("ChairId"); m.ToTable("TableChairs"); }); And everything was working as expected ;)
  • 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.
  • 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.
  • 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 stackoverflow.com/questions/35950795/… (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.