While working with EF code first I get error given below at different times:
The entity type SomeType is not part of the model for the current context.
What are the possible causes of this error?
While working with EF code first I get error given below at different times:
The entity type SomeType is not part of the model for the current context.
What are the possible causes of this error?
It may occur because:
MetaData
property of the EntityConnectionStringBuilder
(in the form of "res://MyAssemblyThatContainsTheModel/"
). I was giving it an assembly that didn't have the entity data model embedded resources and it just built a model with no entities. This was due to me using Assembly.GetCallingAssembly()
which returned an incorrect assembly due to method inlining by the JIT. I marked the inlined methods with [MethodImpl(MethodImplOptions.NoInlining)]
and it fixed the issue. –
Chamorro I got this when my class that inherited from DbContext did not declare the model as a property. For example, I neglected to add a property for FooModel in the code below:
public class MyDBContext : DbContext
{
public DbSet<FooModel> FooModels{ get; set; }
// etc. ...
}
This message also appears if you try to do somthing such a set an EntityState on a child collection in a one-to-many association.
For example; if a one-to-many association exists between ParentEnt and ChildEnt in the code snippet below, the error message:
The entity type Hash1Type is not part of the model for the current context.
MyDbContext.Entry(ParentEnt.ChildEnt).State = EntityState.Unchanged;
The following change does not produce an error:
MyDbContext.Entry(ParentEnd.ChildEnt.First).State = EntityState.Unchanged;
Note that the use of First()
in this case may indicate t
This can also be caused by properties on your POCO not named EXACTLY as they are in the EDMX/modelbuilder. Please see my post here for details on how I trouble shot the issue.
The entity type <class> is not part of the model for the current context
I had this error.
It turned out I had added a new field to a db View a few hours before. I updated the context (as part of something else I was doing) and got this error.
When I updated the POCO's all was well: EF threw this error because it could not map a field in the View to a property in the POCO of the View.
Not the most helpful error message in this situation IMO.
It may happen when your model is not mapped correctly to your Class. In my case I got this error when I used EF Model First and when I updated my EDMX model from DB but didn't update my Entity class. Specifically a property in Entity was in lower case while in DB and EDMX diagram was in Upper case. And another issue I had was a model property in EDMX diagram was not converted to my app Enum So that EF couldn't recognize that Entity.
I've been doing database first and using the built in template generation for my models (EF 4.1)
I copied the generated code into a new file and removed the navigation properties. That's when I started to see this error. I have lazy loading turned off, but it appears the navigation properties are still necessary in the POCO's.
I suppose the error might indicate that your model is missing something.
namespace TestApp.BLL
{
using System;
using System.Collections.Generic;
public partial class User
{
public User()
{
//this.Roles = new HashSet<Role>();
}
public int UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
//public virtual ICollection<Role> Roles { get; set; }
}
}
The above code shows the navigation properties commented out. If I uncomment them on all POCO's (that means the Role POCO too) the exception goes away.
UPDATE
This error kept attacking me with various updates I made to the database. Eventually I deleted the edmx file and just created it again with the same tables and stored procs.
Old
I got this when the generated entity was missing a nullable column:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace MyProgram.Models
{
using System;
using System.Collections.Generic;
public partial class Question
{
public int id { get; set; }
public string title { get; set; }
public string body { get; set; }
public string tags { get; set; }
public int votes { get; set; }//I had to manually add this property which is nullable int in the database
}
}
I added the property after generating the initial model. However, I even tried deleting the table and recreating it. That didn't fix it. Only adding the property manually fixed it for me.
Answering the question is "What are the possible causes of this error?":
This error seems to occur any time the internal/EDMX model is not successfully built, or is not completely built. And there are a large potential number of causes for this problem. It's unfortunate that there seems to be insufficient error reporting or error detection when building the model, so solving it seems to involve trying a bunch of things to see what makes the problem go away.
I ran into another instance of this error over the past few days, using EF 6.0 (currently pre-release code), code-first, and custom conventions. It turns out that the root cause is that I had a custom convention which renames the ID EdmProperty (eg ID -> MyTypeId). If I disabled my custom convention this problem went away; if I enabled my convention the problem occurs. There is no logging or exceptions or other errors to indicate that a problem occurred when the building the model. This exception doesn't rear its head until I try to add an entity to a DbSet. The convention didn't cause any problem when generating the database (via Migrations).
In my scenario I was using EF6 to migrate a MySQL database to MSSQL. I had 2 separate models and contexts, each with their own connection string. The classes had the same name, but the MySQL one was all lowercase and the MSSQL one Pascal casing. I had copied in both connection strings from the relevant assemblies containing my EDMX models. But when I ran my application I got an error about one of the 2 connection strings having been already added to the dictionary list.
So I removed the conflicted entry, foolishly thinking it somehow had access to the connection string in the assembly's own app.config (it's late!). But no, the error was actually happening because both connection strings also had the same name - one all lowercase and one in Pascal casing - i.e. the Dictionary key ignores the casing. So when I removed the MySQL one, it then attempted to use the MSSQL connection string for BOTH models. So I had to re-add, rename and manually set the connection string for the second model in code.
© 2022 - 2024 — McMap. All rights reserved.