Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using System.ComponentModel.DataAnnotations with Entity Framework 4.0

I'm working with MVC3, and using Entity Framework 4.0 Entities as my model. So far, everything works great as far as using it as a model (all the crud operations/page generations work out of the box). I'm wondering, though, how do you get the same robust labels and validation information as when you generate a model manually?

Here's an example of what I mean. This is a class generated by the sample MVC3 project:

public class LogOnModel {     [Required]     [Display(Name = "User name")]     public string UserName { get; set; }      [Required]     [DataType(DataType.Password)]     [Display(Name = "Password")]     public string Password { get; set; }      [Display(Name = "Remember me?")]     public bool RememberMe { get; set; } } 

With the example above, you can specify what gets rendered in a label for the field (Display), and what type of field to use (Password). However, when I try to use the entity framework and push it to the view below, I see the automatically generated labels are just the field names, and not anything I want the user to see/have to read:

@using (Html.BeginForm()) {     @Html.ValidationSummary(true)     <fieldset>         <legend>Person</legend>          <div class="editor-label">             @Html.LabelFor(model => model.FirstName)         </div>         <div class="editor-field">             @Html.EditorFor(model => model.FirstName)             @Html.ValidationMessageFor(model => model.FirstName)         </div>          <div class="editor-label">             @Html.LabelFor(model => model.MiddleName)         </div>         <div class="editor-field">             @Html.EditorFor(model => model.MiddleName)             @Html.ValidationMessageFor(model => model.MiddleName)         </div>          <div class="editor-label">             @Html.LabelFor(model => model.LastName)         </div>         <div class="editor-field">             @Html.EditorFor(model => model.LastName)             @Html.ValidationMessageFor(model => model.LastName)         </div>          <div class="editor-label">             @Html.LabelFor(model => model.Birthdate)         </div>         <div class="editor-field">             @Html.EditorFor(model => model.Birthdate)             @Html.ValidationMessageFor(model => model.Birthdate)         </div>          <p>             <input type="submit" value="Create" />         </p>     </fieldset>} 

enter image description here

My question is: How do I add these extra decorations to the entities that are generated using EF4? Is there something besides System.ComponentModel.DataAnnotations that I should be using? I know entities get regenerated and it's probably not a good idea to add this to entities' code directly, but for some reason I can't think of a better approach than manually entering the label text in the view (lame, there's no reason to have to do that, this is MVC!). I want to keep it so that the application is dynamic enough to be able to have the correct display information for my model come through and keep an MVC approach. How do I do it?

like image 292
Ryan Hayes Avatar asked Feb 06 '11 20:02

Ryan Hayes


People also ask

What is the use of using system ComponentModel DataAnnotations?

Data annotations (available as part of the System. ComponentModel. DataAnnotations namespace) are attributes that can be applied to classes or class members to specify the relationship between classes, describe how the data is to be displayed in the UI, and specify validation rules.

Which namespace is used for data annotation?

ComponentModel. DataAnnotations Namespace. Provides attribute classes that are used to define metadata for ASP.NET MVC and ASP.NET data controls.


2 Answers

I haven't done this for ASP.NET MVC (only for Silverlight) but I believe the same principles would apply. You can create a "metadata buddy class" as below, because the types generated by EF should be partial, thus you can add a bit more to them (like the MetadataTypeAttribute) and then you create this sibling class that holds the metadata.

It's kind of ugly, but should work. It goes something like this (assuming the EF entity is named "Person"):

[MetadataType(typeof(PersonMetadata))] public partial class Person {    // Note this class has nothing in it.  It's just here to add the class-level attribute. }  public class PersonMetadata {   // Name the field the same as EF named the property - "FirstName" for example.   // Also, the type needs to match.  Basically just redeclare it.   // Note that this is a field.  I think it can be a property too, but fields definitely should work.     [Required]    [Display(Name = "First Name")]   public string FirstName; } 
like image 199
Austin Lamb Avatar answered Oct 10 '22 23:10

Austin Lamb


Same as above but with all the details, and it works

enter image description here

enter image description here

enter image description here

enter image description here

And Here is the Code

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations;  namespace Validate.Models {     [MetadataType(typeof(PersonMetadata))]     public partial class Person     {         // Note this class has nothing in it.  It's just here to add the class-level attribute.     }      public class PersonMetadata     {         // Name the field the same as EF named the property - "FirstName" for example.         // Also, the type needs to match.  Basically just redeclare it.         // Note that this is a field.  I think it can be a property too, but fields definitely should work.          [Required]         [Display(Name = "Enter Your Name")]         public string FirstName;     } } 
like image 27
Arun Prasad E S Avatar answered Oct 11 '22 01:10

Arun Prasad E S