Entity Framework: Unknown column innerexception

When working with Entity Framework dbsets and using a generic repository pattern, in your repository base class you may have methods such as:

public virtual IEnumerable<T> GetAll()

and in these functions access “dbset” objects.  If you get an error that says your particular dbset contains an unknown column, it may be that another dbset object defines a virtual reference to the object complaining.

Example Problem:

Class User

Class Profile

You may get an error when doing a UserRespository.GetAll() call and the innerexception may say something like “Profile_ID not a part of User”.  Then you search your User class only to find that there is no mention of Profile_ID nor any reference to Profile!

Solution:  But when you peak inside of the Profile object, you’ll find a virtual parameter of type User.  This will automatically generate a new parameter in the User object and since it doesn’t exist in the database table, you get the exception.

public virtual ICollection<User> Users { get; set; }

Either remove the reference from Profile (in this case) or add a foreign key relationship in your database in the User table that links to Profile.

Posted in Uncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *