EF 4.1 Code First error - The entity type SomeType

2019-01-18 08:04发布

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?

10条回答
乱世女痞
2楼-- · 2019-01-18 08:58

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.

查看更多
祖国的老花朵
3楼-- · 2019-01-18 09:00

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.

查看更多
疯言疯语
4楼-- · 2019-01-18 09:01

It may occur because:

  • DbContext configured with an incorrect connection string
  • The entity specified is actually not mapped in configuration
查看更多
够拽才男人
5楼-- · 2019-01-18 09:05

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.

查看更多
登录 后发表回答