Which approach is better to define Data Annotation (View Model or Entity )? [closed]

陌路散爱 提交于 2019-12-12 20:05:34

问题


Better approach to define Data annotation in entity or view model ?

I have entity and view model for the same entity and override the class with Data Annotation of entity framework. The question is i can add them without creating a separate model however i have concern about which one better approach?

Thank you in advance.

Entity

[Table("BatchInfo")]
    public partial class BatchInfo
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int BatchInfoId { get; set; }
        public string TRANCODE { get; set; }
        public string BatchId  { get; set; }
        public bool AMTFLAG { get; set; }
        public int POS { get; set; }

    }

View Model

 [NotMapped]
    public class BatchInfoViewModel:BatchInfo
    {
        [Required(ErrorMessage = "Transaction Code is required")]
        [Remote("CheckTranCode", "BatchInfo",AdditionalFields = "BatchInfoId", ErrorMessage = "Transaction Code Already Exists!")]
        [RegularExpression("[A-Za-z0-9^]*", ErrorMessage = "Invalid Transaction Code")]
        [StringLength(3, ErrorMessage = "Transaction Code Should Not be More than 3 Characters")]
        public new string TRANCODE { get; set; }
        [Required(ErrorMessage = "Batch Code is required")]
        [RegularExpression("[A-Za-z0-9^]*", ErrorMessage = "Invalid Batch Code")]
        [StringLength(5, ErrorMessage = "Transaction Code Should Not be More than 5 Characters")]
        public new  string BatchId { get; set; }
        [Required(ErrorMessage = "Position is required")]
        [RegularExpression("^[0-9]*$", ErrorMessage = "Only Number is allowed")]
        [Range(0, int.MaxValue, ErrorMessage = "Invalid Position")]
        public new  int? POS { get; set; }
}

来源:https://stackoverflow.com/questions/50857548/which-approach-is-better-to-define-data-annotation-view-model-or-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!