Adding Validation Attributes With an Entity Framework Data Model

前端 未结 5 1741
春和景丽
春和景丽 2020-11-30 03:06

Preface Feb 2015 If you are still using Entity Framework EDMX, please do yourself a favor and checkout using Entity Framework Code First instead. The differ

5条回答
  •  心在旅途
    2020-11-30 04:00

    There is a variation of the suggested answers here that allows you to use classes in different assemblies and namespaces. I haven't actually tested it with EF, but I am using this for Swagger codegen API model classes.

    In a nutshell: inherit from the model class and add metadata on the inherited class. An added benefit is that with Swagger codegen you can use the API model directly without mapping and for initial forms you can use the protected default ctor.

    [MetadataType(typeof(LocalAssemblyModelMetadata))]
    public class LocalAssemblyModel : IO.Swagger.Model.OtherAssemblyModel 
    {
        public LocalAssemblyModel() : base ()     { }
    }
    
    
    
    public sealed class LocalAssemblyModelMetadata
    {
        [Required(ErrorMessage = "BaseclassProperty is mandatory.")]
        public string BaseclassProperty { get; set; }
    }
    

提交回复
热议问题