Reputation: 3044
I’d like to know if I could customize my entities in EF which don’t necessarily correspond to all fields in the database table. This way I can make use of the built in MVC validation features.
Here’s a simplified example of what I’m trying to do. So let’s say I have a Person table defined as:
-Id,
-Name,
- Place of birth.
In the View, the user would type in its name, and then select the country of birth and then its location. There is no need to store the country of birth in the person table as a location belongs to a country, but the country needs to be selected in the view so the location options are populated.
The country is therefore a required value in the view.
I know I could manually validate this required value on the server, but I’d like to know if I could define an extra property in the EF entity called CountryId which would be marked as required and then I get all the default MVC validation features.
If this is a common practice links to helpful tutorials would be appreciated.
Thanks
Upvotes: 0
Views: 56
Reputation: 761
I would say you can use MVVM pattern, to split your ViewModels which can differ of you data models. So you could set validation attributes on it. The approach is described in more details in this blog post: The idea behind it is to get your ViewModel to validate it in some way and to put your data into appropriate fields of your business model. You can use some kind of Mapper solution with it. AutoMapper is a good one to start with.
Upvotes: 3