DisplayName for Entity Framework Model

I have a code that looks like this:

public class MyModel { [Required] [Display(Name = "labelForName", ResourceType = typeof(Resources.Resources))] public string name{ get; set; } } 

The problem is that the Display and Required attribute has been added to the generated Entity Framework model class. I know that I can add functionality with Partial, but how to add an attribute to a class that will be deleted and updated using ORM?

+6
c # asp.net-mvc asp.net-mvc-3
source share
2 answers

In my experience, database models are rarely the same as on web pages. You always need some kind of change. Hence the use of ViewModel s. Another drawback is that all web pages using your ViewModel will not be broken if the entity model is changed.

In terms of security, if you have a public ActionResult Save(MyEntityModel model) , this can lead to a security violation, as the user can figure out how to send values ​​to properties that should not be changed (for example, Role , Status , IsAdmin or something still).

Instead, check out the mapper (e.g. automapper ) and put the attributes in the ViewModel.

+8
source share

Do you find the T4 template to modify the generated code.

http://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx

I try to use T4 templates in combination with partial classes when working with generated code.

+3
source share

All Articles