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

后端 未结 10 1481
遇见更好的自我
遇见更好的自我 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: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();
            }
    
            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 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.

提交回复
热议问题