The Entity Type <Type> Is Not Part of the Model for the Current Context

The entity type type is not part of the model for the current context

Put this in your custom DbContext class:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Estate>().ToTable("Estate");
}

If your tables are not created on startup, this is why. You need to tell the DbContext about them in the OnModelCreating method override.

You can either do custom per-entity mappings here, or separate them out into separate EntityTypeConfiguration<T> classes.

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

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?

why I got error The entity type LAB_INVOICE_VIEW is not part of the model for the current context.?

I deleted the EDMX file and created new EF again and added all models , This solved my issue .

Thank you Mr Gert

the entity type is not part of the model for the current context error is thrown when project contains more than one EDMX file


Workaround: Change a property on one of the two identical classes.

EF matches on class name AND class properties. So I just changed a
property name on one of the EF objects, and the error is gone.

As @Entrodus commented on one of the other answers:

EF collision happens only when two classes have the same name AND the
same set of parameters.

The mapping of CLR type to EDM type is ambiguous with EF 6 & 5?

The entity type ApplicationUser is not part of the model for the current context. Used two different databases at beginning of project

I have tried to reproduce your issue according with below steps:

1) create Asp.net MVC template, then register a new user.

Result: We could find user info on local database.

2) Add controller with views using Entity Framework. And use Azure SQL database as its resource.

Result: we will find two connection in our web.config

3) Delete default connection string

4) Change Application DB Context connection string

   <add name="jambdbEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=tcp:jambdb.database.windows.net,1433;initial catalog=jambdb;user id=jambor;password=***;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
 public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("jambdbEntities", throwIfV1Schema: false)
{
}

public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}

After above steps, My application give me below error:

Sample Image

Solution:

1) Edit 'DefaultConnection' connection string

  <connectionStrings>
<add name="jambdbEntitiesapplication" providerName="System.Data.SqlClient" connectionString="Server=tcp:jambdb.database.windows.net,1433;Initial Catalog=jambdb;Persist Security Info=False;User ID=jambor;Password=***;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" />
<add name="jambdbEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=tcp:jambdb.database.windows.net,1433;initial catalog=jambdb;user id=jambor;password=***;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>

2) Modify the code:

 public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("jambdbEntitiesapplication", throwIfV1Schema: false)
{
}

public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}

3) Modify AutomaticMigrationsEnabled = true; in Configuration class under Migrations folder.

Here is the result:

Sample Image



Related Topics



Leave a reply



Submit