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