Getting "The entity type <model> is not part of the model for the current context." Getting "The entity type <model> is not part of the model for the current context." asp.net asp.net

Getting "The entity type <model> is not part of the model for the current context."


This always happened if your repository needs to dynamic accessing different Entity Framework DbContext which means different databases.

Check your data connection string in web.config file for each Entity Frmework DbContext.

For example:

 <add name="CRMEntities" connectionString="metadata=res://*/CRMEntities.csdl|res://*/CRMEntities.ssdl|res://*/CRMEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=Your Data Source;initial catalog=CRM;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />

check metadata in connectionString if it is pointing to a correct DbContext.

In this example, it is pointing to demx file called "CRMEntities".


The error comes from how you initialize the data context db.

The user object has been created in a separate db, so, when you are trying to update user, the current db doesn't know about this user object.

You could solve it by getting a user

try{    // or check on FirstName and LastName if you don't have a user id    var updatedUser = db.Users.SingleOrDefault(x => x.id == id);    updatedUser.FirstName = user.FirstName;    updatedUser.LastName = user.LastName;    db.Entry(updatedUser).State = EntityState.Modified;    db.SaveChanges(); }

Alternatively, you could make sure that the data context you are using to create the user object is the same as the one that is trying to update the user.

Does this make sense to you?


Make sure you have the correct metadata part should be same as in edmx.

connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;"