DisplayName on Model that use Entity Framework

13,192

Solution 1

In my experience, models from a database are seldom the same as being used in web pages. You always need some kind of change. Hence the usage of ViewModels. Another upside is that all web pages that use your ViewModel won't break if the entity model is changed.

As for security, if you have a public ActionResult Save(MyEntityModel model) can lead to a security breach since the user may figure out how to post values to properties that shouldn't be changed (like Role, Status, IsAdmin or whatever).

Get yourself familiar with a mapper (like automapper) instead, and put the attributes on the ViewModel.

Solution 2

Have you considered a T4 Template to modify generated code.

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

I tend to use T4 templates in combination with partial classes when dealing with generated code.

Share:
13,192
Patrick Desjardins
Author by

Patrick Desjardins

Senior Software Developer at Netflix [California, Los Gatos] Senior Software Developer at Microsoft [Washington, Redmond] Working for Microsoft Cloud and Enterprise, mostly on Team Services Dashboards, Kanban and Scaled Agile project Microsoft Teams (first release) Microsoft MVP 2013 and 2014 [Quebec, Montreal]

Updated on June 13, 2022

Comments

  • Patrick Desjardins
    Patrick Desjardins almost 2 years

    I have a code that look like this :

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

    The problem is the attribute Display and Required have been added in the generated model class of Entity Framework. I know I can add functionality with Partial but how can I add attribute to a class that will be erase and updated with the ORM?

  • Patrick Desjardins
    Patrick Desjardins almost 13 years
    I am reading about it, doesn't want to make it too complex too. I may try to find an easier way.
  • Thomas
    Thomas almost 13 years
    There is a learning curve but its worth it. There is a powerful template used for MVC application on codeplex. Digg through there example im sure you will find some useful nuggets. mvccontrib.codeplex.com/wikipage?title=T4MVC If you want I could write you up and example of how to modify an attribute.
  • Patrick Desjardins
    Patrick Desjardins almost 13 years
    I have watched a screencast about AutoMapper and I see that it can do stuff methods but not adding attribute. Am I wrong?
  • jgauffin
    jgauffin almost 13 years
    Sorry, I was a bit unclear. Automapper just copies information from your entity to your ViewModel. You have to add the attributes to your viewmodel manually.
  • Patrick Desjardins
    Patrick Desjardins almost 13 years
    Yeah but because the AutoMapper just do that : OrderDto dto = Mapper.Map<Order, OrderDto>(order); I cannot add to OrderDTO some attribute on the method like the [Display(...)].
  • jgauffin
    jgauffin almost 13 years
    huh? Why not? It's the OrderDTO that is used in the web. Using validation attributes and display attributes on it makes perfect sense.
  • Patrick Desjardins
    Patrick Desjardins almost 13 years
    You are right, the documentation is very light... hard to get how the automapper library work :P I'll check more the screencast, all seem to be in it.