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.
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.
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= "data source=(localdb)\v11.0; initial catalog=Blogging; integrated security=True; multipleactiveresultsets=True;"" 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> 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; } 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.
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.
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.
My problem was resolved by updating part of the connection string metadata. Obviously this was pointing to the wrong .csdl / .ssdl / .msl link.
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.
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.
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.
That sounds obvious, but make sure you don't explicitly ignore the type:
modelBuilder.Ignore<MyType>();
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.
This can also occur in a type structure different from the entity, for example, from incorrect data and / or missing fields mismatching.
if you are trying to use the database first, make sure your table has a primary key
Visual Studio 2019 seems to be causing this for me. I fixed this by generating the edmx model again in 2017.