How to add data annotations to partial class?

ε祈祈猫儿з 提交于 2019-11-30 05:14:41

Looks like I figured out a different way similar to the link above using MetadataTypeAttribute:

namespace MyApp.BusinessObjects
{
    [MetadataTypeAttribute(typeof(SomeClass.Metadata))]{
    public partial class SomeClass
    {
        internal sealed class Metadata
        {
            private Metadata()
            {
            }

            [Required]
            public string Name{ get; set; }
        }
    }
}

I'm using the below to also support multiple foreign keys in the same table that refer to the same table. Example, the person has two parents (Father and Mother) who are both Person class.

[MetadataTypeAttribute(typeof(SomeClassCustomMetaData))]
public partial class SomeClass
{

}

public class SomeClassCustomMetaData
{
    [Required]
    public string Name { get; set; }
    [InverseProperty("Father")]
    public virtual Parent ParentClass { get; set; }
    [InverseProperty("Mother")]
    public virtual Parent ParentClass1 { get; set; }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!