Error in EF code first: Invalid column name Discriminator

Reprinted: https://blog.csdn.net/lanse_my/article/details/38128355


I ran into an error the other day using code first: The column name 'Discriminator' is invalid. This is a rare error, and after searching for a long time, I found that it is an inheritance problem of the code first poco entity object.

For example, I define an entity class that corresponds to the Project table of the database :

public class Project
{
int Id { getset; }
string Name { getset; }
}

Later, a subclass ChildProject was defined to inherit from it, but the subclass does not correspond to any table in the database

public class ChildProject : Project
{
}

So every time EF's Context accesses Project or ChildProject, an error occurs: the column name 'Discriminator' is invalid.

Solution: The subclass does not map to any database, just add an unmapped attribute [NotMapped].

[NotMapped]
public class ChildProject : Project
{
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324838870&siteId=291194637