Using System.ComponentModel.DataAnnotations with Entity Framework 4.0

前端 未结 3 1635
北恋
北恋 2020-11-27 04:00

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 gene

相关标签:
3条回答
  • 2020-11-27 04:17

    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;
    }
    
    0 讨论(0)
  • 2020-11-27 04:26

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

    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;
        }
    }
    
    0 讨论(0)
  • 2020-11-27 04:26

    Like Austin Lamb's answer, but instead, nesting the MetaData class within the entity class, thereby reducing the number of classes in your public namespace list, and eliminating the need to have a unique name for each metadata class.

    using System.ComponentModel.DataAnnotations;
    
    namespace Validate.Models
    {
        [MetadataType(typeof(MetaData))]
        public partial class Person
        {
            public class MetaData
            {
                [Required]
                [Display(Name = "Enter Your Name")]
                public string FirstName;
    
                //...
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题