Using System.ComponentModel.DataAnnotations with Entity Framework 4.0

前端 未结 3 1658
北恋
北恋 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条回答
  •  萌比男神i
    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;
        }
    }
    

提交回复
热议问题