entity-frameworktable-per-hierarchy

EF Table per Hierarchy (TPH) not saving because can't insert value null in Discriminator column


I have a table used for multiply type of category and it contains a Discriminator column named 'ClassName' to specify the type of object to load. The ClassName column is non nullable with a default value of 'Category'

My problem is when saving a new item , I get the error : 'Cannot insert the value null into column ClassName' table Category.

I tought that ef would set the ClassName value base on the new object class. How can I save my object with the right 'ClassName' value ?


Solution

  • I changed my db Structure to accept null. EF will set null if the object name match the table name and will set the discreminator name for the derived classes.