Let me start telling the problem: When I submit my View, my Action receives a object from a class called Customer that have foreign keys that are integer but should be nullable.
The class was generated by EF Designer, and the structure of my database is the following (I wish put an image, but, it tells me that I need 10 reputation):
Customer (Table)
CityId -> City (Table)
CityId
StateId -> State (Table)
StateId
CountryId -> Country (Table)
CountryId
Resuming, I have a Customer that must be in a city, that must be related with a state, that must be related with a Country.
In other hand, I have my View that has 3 Dropdowns: 1 to choose the country, another to choose one state from the selected country, and the third one that is to choose the City from the selected state. When I select the Country, the View (via JSON), fill another dropdown with the states, etc, so, it's obviously that I save only the CityId.
The problem is, when I try submit, the MVC shows me the following validation:
The CountryId field is required.
The StateId field is required.
The CityId field is required.
It occurs because those fields are Int32. So, the first thing that I did was change, in the EF Designer those fields to nullable (because I want to put a personal validation via ModelState.AddModelError).
I also changed the Multiplicity to 0..1.
In the database, those fields must to be non nullable.
But now, I am getting the following error: Error 3 Error 3031: Problem in mapping fragments starting at line 1074:Non-nullable column Cidades.EstId in table Cidades is mapped to a nullable entity property.
What is the best way to fix this?
Thanks in advance