开发者

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

开发者 https://www.devze.com 2023-04-07 10:45 出处:网络
I have a table used for multiply type of category and it contains a Discriminator column named \'ClassName\' to specify the type of object t开发者_如何学Pythono load. The ClassName column is non nulla

I have a table used for multiply type of category and it contains a Discriminator column named 'ClassName' to specify the type of object t开发者_如何学Pythono 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)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号