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

后端 未结 10 1480
遇见更好的自我
遇见更好的自我 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 01:13

    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.

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

    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.

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

    It may occur because:

    • DbContext configured with an incorrect connection string
    • The entity specified is actually not mapped in configuration
    0 讨论(0)
  • 2020-12-10 01:20

    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.

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