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

I end up in the Entity Framework, but I'm not sure that I am missing a critical point in the code approach.

I use the generic repository template based on the code https://genericunitofworkandrepositories.codeplex.com/ and created my entities.

But when I try to access or modify the object, I run the following:

System.InvalidOperationException: The type of the Estate object is not part of the model for the current context.

This happens when I try to access it from my repository:

public virtual void Insert(TEntity entity) { ((IObjectState)entity).ObjectState = ObjectState.Added; _dbSet.Attach(entity); // <-- The error occurs here _context.SyncObjectState(entity); } 

The database (./SQLEXPRESS) is created just fine, but entities (tables) are simply not created at startup.

I am wondering if entity mapping should be explicitly specified? Can't EF do this on its own?

My essence:

 public class Estate : EntityBase { public int EstateId { get; set; } public string Name { get; set; } } 

My context is this:

 public partial class DimensionWebDbContext : DbContextBase // DbContextBase inherits DbContext { public DimensionWebDbContext() : base("DimensionWebContext") { Database.SetInitializer<DimensionWebDbContext>(new CreateDatabaseIfNotExists<DimensionWebDbContext>()); Configuration.ProxyCreationEnabled = false; } public new IDbSet<T> Set<T>() where T : class { return base.Set<T>(); } } 

Is there any specific reason for this error? I tried turning on migration and turning on automatic migration without any help.

+131
c # entity-framework dbcontext ef-code-first
Dec 19 '13 at 18:21
source share
15 answers

Put this in your own DbContext class:

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

If your tables are not created at startup, here's why. You must tell DbContext this in an override of the OnModelCreating method.

Here you can either create custom mappings for each object, or select them in separate EntityTypeConfiguration<T> classes.

+129
Dec 19 '13 at 18:41
source share

Apparently, this error is very general, it can have a number of reasons. In my case, it was the following: The connection string (in Web.config) generated by .edmx was invalid. After almost a day of trying everything, I changed the connection string from the EF line to the ADO.NET line. This solved my problem.

For example, an EF line looks something like this:

 <connectionStrings> <add name="BlogContext" connectionString="metadata=res://*/BloggingModel.csdl| res://*/BloggingModel.ssdl| res://*/BloggingModel.msl; provider=System.Data.SqlClient provider connection string= &quot;data source=(localdb)\v11.0; initial catalog=Blogging; integrated security=True; multipleactiveresultsets=True;&quot;" providerName="System.Data.EntityClient" /> </connectionStrings> 

And the ADO.NET line looks like this:

 <connectionStrings> <add name="BlogContext" providerName="System.Data.SqlClient" connectionString="Server=.\SQLEXPRESS;Database=Blogging; Integrated Security=True;"/> </connectionStrings> 

Source: http://msdn.microsoft.com/nl-nl/data/jj556606.aspx

+68
Jun 12 '14 at 6:51
source share

For me, the problem was that I did not include the Entity class in my db set inside the context for the entity framework.

 public DbSet<ModelName> ModelName { get; set; } 
+13
Nov 02 '16 at 19:19
source share

The problem may be in the connection string. Make sure your connection string is for the SqlClient provider, without the metadata material related to EntityFramework.

+8
Jun 19 '14 at 15:34
source share

You can try to remove the table from the model and add it again. You can do this visually by opening the .edmx file from Solution Explorer.

Steps:

  • Double-click the .edmx file in Solution Explorer
  • Right-click on the heading of the table you want to delete and select "Delete from Model"
  • Now again, right-click on the workspace and select "Update Model From Database."
  • Add a table from the list of tables
  • Clean and build the solution.
+8
Oct 05 '15 at 2:48
source share

I saw this error when an existing table in the database does not map to the first code model. In particular, I had char (1) in the database table and char in C #. Changing the model to a string resolved the issue.

+3
Jan 09 '17 at 22:44
source share

My problem was resolved by updating part of the connection string metadata. Obviously this was pointing to the wrong .csdl / .ssdl / .msl link.

+3
Apr 08 '19 at 16:59
source share

Another thing to check with your connection string is the model name. At first I used two entity models, a DB. In the config, I copied the entity connection for one, renamed it and changed part of the connection string. What I did not change was the name of the model, so while the entity model was generated correctly when the context was triggered, EF was looking for the wrong model for the objects.

It looks obvious, but four hours I won’t return.

+2
Apr 29 '17 at 2:43 on
source share

For me, the problem is that I used the connection string generated by the ADO.Net Model (EDMX -). Changing the connection string solved my problem.

+1
Jun 29 '18 at 21:04
source share

This can also happen if you use a persistent model cache that is deprecated for one reason or another. If your context has been cached to an EDMX file in the file system (via DbConfiguration.SetModelStore), then OnModelCreating will never be called because the cached version will be used. As a result, if there is no entity in your cached storage, you will receive the above error, even if the connection string is correct, the table exists in the database and the entity is configured correctly in your DbContext.

+1
Jan 22 '19 at 13:27
source share

That sounds obvious, but make sure you don't explicitly ignore the type:

modelBuilder.Ignore<MyType>();

0
Aug 14 '15 at 18:05
source share

displaying an object (even empty) added to the configuration will cause the type of the object to be part of the context. We had an entity that was not related to other objects that were installed with a blank card.

0
Mar 22 '17 at 16:36
source share

This can also occur in a type structure different from the entity, for example, from incorrect data and / or missing fields mismatching.

0
May 03 '17 at 10:24 pm
source share

if you are trying to use the database first, make sure your table has a primary key

0
Jun 18 '18 at 13:49
source share

Visual Studio 2019 seems to be causing this for me. I fixed this by generating the edmx model again in 2017.

0
Jul 05 '19 at 14:14
source share



All Articles