How to create Discriminator column in Database First

I have a Database First approach to my application and Entity Framework. I have a base class that many other classes inherit. I am trying to save the class to my EF database, however, I keep getting an error that there is no Discriminator column.

System.Data.SqlClient.SqlException: Invalid column name 'Discriminator'.

Since I am using Database First, how do I manually create this column in my table? I cannot seem to find the datatype of it anywhere.


The entity framework would try to check for the column in all of the derived classes (isn't that what inheritance is all about?). You can try adding [NotMapped] attribute to your child classes.

[NotMapped]
public class ChildClass : ParentClass {
    // other stuff here
}

This would minimize your problem. This attribute tells Entity framework which of the properties (if applied to fields) or classes do not need to be mapped. Read more about it: https://msdn.microsoft.com/en-us/data/jj591583.aspx?f=255&MSPPError=-2147217396#NotMapped

链接地址: http://www.djcxy.com/p/83240.html

上一篇: 每个层次结构的实体框架表不会创建判别器

下一篇: 如何在数据库中创建Discriminator列