裸体分类

时间:2012-07-02 06:44:38

标签: c# asp.net data-annotations partial-classes

我正在尝试使用Partial Class添加数据注释。

正如您所看到的,我在部分类中添加了一个测试属性,因此我可以测试它是否真的与其他部分相匹配 (如下文http://msdn.microsoft.com/en-us/library/ee256141.aspx

我的班级似乎是一个赤裸裸的部分班级,所以我不确定我在这里做错了什么。

问题是MetaData不适用于部分类(因此忽略了部分类)

你可以帮帮我吗?感谢

    using System;
        using System.Collections.Generic;

        namespace MyProject.Models
        {

public partial class ReAdvSlot
            {
// Poco
                public int AdvSlotId { get; set; }
                public string Name { get; set; }
                public string Description { get; set; }
                public bool IsPublished { get; set; }
                public string Code { get; set; }
                public string Notes { get; set; }
            }

        }
        using System.ComponentModel.DataAnnotations;

        namespace MyProject.Models
        {
            [MetadataType(typeof(ReAdvSlotMetaData))]
            public partial class ReAdvSlot
            {
                public class ReAdvSlotMetaData
                {
                    public int AdvSlotId { get; set; }
                    public string Name { get; set; }
                    public string Description { get; set; }
                    public bool IsPublished { get; set; }
                    public string Code { get; set; }
                    public string Notes { get; set; }
                    public string TestProperty { get; set; } // TEST PROPERTY
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

不会忽略部分类。如果您将Test属性放入实际的分部类而不是元数据中,您将在类定义中看到它。

    namespace MyProject.Models
    {

        public partial class ReAdvSlot
            {
                public int AdvSlotId { get; set; }
                public string Name { get; set; }
                public string Description { get; set; }
                public bool IsPublished { get; set; }
                public string Code { get; set; }
                public string Notes { get; set; }
            }

        }
    }

    namespace MyProject.Models
    {
        [MetadataType(typeof(ReAdvSlotMetaData))]
        public partial class ReAdvSlot
        {
             public string TestProperty { get; set; } // TEST PROPERTY here instead
        }

        public class ReAdvSlotMetaData
            {
                [Required] //Example of defining metadata
                public int AdvSlotId { get; set; }
                public string Name { get; set; }
                public string Description { get; set; }
                public bool IsPublished { get; set; }
                public string Code { get; set; }
                public string Notes { get; set; }

            }
    }
相关问题