How to add data annotation for entities automatically created by Data-First?

前端 未结 3 1420
-上瘾入骨i
-上瘾入骨i 2020-12-28 23:53

If model-first, we use [MetadataType(typeof(ConceptMetadataSource))] to attach a MetadataSource file which contains all the data annotations like [Hidden

相关标签:
3条回答
  • 2020-12-29 00:19

    All you have to do is create another partial class and use metadatatype attribute. Here is the sample code

    //This is generated by EDMX
    
    namespace DataLayer
    {
        using System;
        using System.Collections.Generic;
    
        public partial class Customer
        {
            public Customer()
            {
                this.CustomerAddresses = new HashSet<CustomerAddress>();
                this.CustomerOrders = new HashSet<CustomerOrder>();
            }
    
            public int Id { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string EmailId { get; set; }
    
    
            public Nullable<System.DateTime> DateOfBirth { get; set; }
    
            public virtual ICollection<CustomerAddress> CustomerAddresses { get; set; }
            public virtual ICollection<CustomerOrder> CustomerOrders { get; set; }
        }
    }
    

    Add following code manually

    namespace DataLayer
    {
        [MetadataType(typeof(CustomerMetaData))]
        public partial  class Customer
        {
    
        }
        public class CustomerMetaData
        {
            [StringLength(10, ErrorMessage = "First name must be 25 characters or less in length.")]
            [Required(ErrorMessage = "First name is required.")]
            public String FirstName { get; set; }
        }
    }
    
    0 讨论(0)
  • 2020-12-29 00:32

    define a view model like

    public class VMConcept
    { 
        public Concept NewConcept {get; set;}
    }
    
    [MetadataType(typeof(ConceptMetadataSource))]
    public partial class Concept{}
    
    public class ConceptMetadataSource {
    
     [Required(ErrorMesssage="This Field is required")]
     public string PropertyName {get; set;}
    }
    
    0 讨论(0)
  • 2020-12-29 00:39

    Okay, here is the answer.

    The trick is, the auto-generated classes are all partial classes. The compilation process will combine all partial classes with the same name.

    If we have public partial class Concept generated by DbContext, all we need to do is to create another one started with public partial class Concept. This new partial class can be created in a different folder, but we need to its namespace should be updated into the same as the auto-generated partial class.

    In this newly created partial class, we can add all kinds of data-annotations such as

    [Required(ErrorMesssage="This Field is required")]
    

    Or, we can even add new properties like

    FullName {get {return string.Format("{0} {1}", FirstName, LastName);}}
    

    If the model is updated from the database again, only the auto-generated partial classes will be updated. Those newly manually added partial classes, which contain our annotations and other manipulations will remain intact.

    0 讨论(0)
提交回复
热议问题