EF 4.1 Code First error - The entity type SomeType is not part of the model for the current context

后端 未结 10 1479
遇见更好的自我
遇见更好的自我 2020-12-10 00:28

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.

相关标签:
10条回答
  • 2020-12-10 00:57

    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.

    0 讨论(0)
  • 2020-12-10 01:01

    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

    0 讨论(0)
  • 2020-12-10 01:03

    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. ... 
    }
    
    0 讨论(0)
  • 2020-12-10 01:05

    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.

    0 讨论(0)
  • 2020-12-10 01:08

    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

    0 讨论(0)
  • 2020-12-10 01:12

    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).

    0 讨论(0)
提交回复
热议问题