0

I'm new to MVC & EF but I am getting a "ConstraintException This property cannot be set to a null value" on a particular entity when I test the client side validation. I generated the ef model from an already existing database. The table in question (ItemTypes) has a primary key that is referenced in 2 other tables (Items and ItemTypeAttributes respectively).

If I try to create an Item after filling in no fields I get a client side validation firing off, the text box turns red and i get the correct validation message. Now if I try to create an ItemType without entering a name, I get the ConstraintException, and THEN after hitting resume in Visual Studio, I see the client side validation error message and red text box.

The message says to relax or turn off constraints in the Dataset, be sure you are not trying to assign a value to a primary key field ... and clear datasets before loading them from view state.

Please advise.

-- edit --- about 20 minutes later I decided to remove the foreign key constraint from the db and update model from database. This works but it's not what I ultimately want. I would like to have referential integrity between the tables. So I can continue testing other parts of the application but I'm sure there are other relationships in my schema that I will come across with similar issues.

tereško
  • 58,060
  • 25
  • 98
  • 150
markS
  • 580
  • 5
  • 14

1 Answers1

0

I had a similar problem and after further investigation was able to solve this by ensuring the properties in my entity were set to be nullable.

See my answer for further details.

Community
  • 1
  • 1
Benjamin Gale
  • 12,977
  • 6
  • 62
  • 100