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

StackOverflow https://stackoverflow.com/questions/7558397

سؤال

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 ?

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

It's old question but I just met it today. In .net 4.0 code first I don't meet "disclaimer column" but when downgrade to net 3.5, it make me tired for a day. This is my solution

Change the column in the database to ALLOW NULL (using alter table) and update the edmx file to make it update the change(allow null)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top