简体   繁体   中英

MVC data annotations

I'm using entity framework. It auto generated an Answer class. But I cannot modify this class as it will be overwritten if code is regenerated.

在此处输入图片说明

Is there a way/technique to add data annotations using this class?

The reason I ask is that I have a Create Answer view that uses the Answer class. Hence, I want to add data annotations such as Required, etc.

If I cannot add data annotations to the Answer class, then I have to create an "AnswerDuplicate" class, pass that into the view, and also use that in my Create() method. However, I then have to map all the values from AnswerDuplicate to an instance of Answer, since Answer is what I "add" to the entity.

Seems like extra work since Answer is autocreated (class and the binding in Create() method). Would be nice if I can just add data annotations.

If this is not possible, then is my only option to create an "AnswerDuplicate" class and use that in place of the Answer class?

My suggestion is: Use ViewModels .

I always consider creating a ViewModel when editing/viewing data - rather than passing the model object directly down to the view.

How Will This Help?
The logic to display it in the view (with Required , DataType and validation such like) can be left down to the ViewModel; while your Model is just left as a normal all-intents-and-purposes class (in your case, a generated one).

As an example, you could have an AnswerViewModel , which contains all of your display/validation logic, to then use in your corresponding View.


How Do I Save Time Mapping Properties?
You can use AutoMapper (or other similar auto-mapping packages) to then automatically map the properties between your model and the view model, for easy updating of entities etc.

This then saves you time having to write lines-upon-lines of code to update entities - which may essentially need to change over time - this can be a big problem (and a huge PITA) if refactoring/adding extra properties across different classes.


How Does This Help Going Forward?
Well, because you are not leaving the logic up to your class:

Let's say you have 3 different views for different purposes (Add, Edit, Delete)

If (for some reason) you need to show/display something differently in only one particular view, you are able to just apply/change the logic in the relevant ViewModel; rather than worrying about updating the Model and having breaking changes affect everything else.


Here is a handy tutorial on How To Use ViewModels In MVC : http://rachelappel.com/use-viewmodels-to-manage-data-amp-organize-code-in-asp-net-mvc-applications/

I do hope this helps, somewhat :)

If you need me to provide any examples - just let me know.

I think the best solution is to use ViewModels as @Geoff James said, but if you don't like to add different classed you can write a partial class and add the MetadataType attribute to it and the add the attributes you want like Required to its properties.

public partial class Answer  // this is auto-generated
{
    public long AnswerID {set; get;}
}

[MetadataType(typeof(Answer ))]
public partial class Answer // this is what you can write
{
    [Required]
    public long AnswerID {set; get;}
}

remember that both class must have a same namespace.

And other solution to your problem is the you can switch to Code First

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM