0

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 ?

freddoo
  • 6,770
  • 2
  • 29
  • 39
  • 1
    Yes, EF should care about the `Discriminator` column values in TPH mapping. I think you must show your code because something is likely not correct in your entities or mappings. Do you have for example configured that the discriminator column name is `ClassName` (which is not default)? – Slauma Sep 26 '11 at 21:56
  • I use the UI to set my discriminator in the edmx, Right click the table, select table mapping, In Maps to Category i set when ClassName = CategoryRoot it works fine and create my CategoryRoot object, but from my category when i add a new category the classname value is set to null, i want is set to Category. I will lookup how to set the discriminator – freddoo Sep 27 '11 at 17:46

2 Answers2

1

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.

freddoo
  • 6,770
  • 2
  • 29
  • 39
0

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)

Andiana
  • 1,912
  • 5
  • 37
  • 73