数据注释元数据类型不起作用

发布于 2024-12-26 12:46:54 字数 1879 浏览 2 评论 0原文

我无法将 MetadataType 附加到我们的应用程序中自动生成的类。 我测试了在生成的类中设置 Order 属性,它工作正常,但如果尝试使用另一个类,我以后将无法获取属性。

我也已经尝试了此处建议的解决方案,没有成功。

生成的类

[Table(Name = "T_MKT_Product")]
public partial class T_MKT_Product : GlobalSist.DataAccess.Base.BaseEntity
{
    [Column(Storage = "_code", DbType = "varchar(20)", IsUnique = true)]
    public virtual string Code
    {
        get { return _code; }
        set
        {
            if (_code != value)
            {
                OnPropertyChanging("Code");
                _code = value;
                OnPropertyChanged("Code");
            }
        }
    }

    [Column(Storage = "_name", DbType = "varchar(200)")]
    public virtual string Name
    {
        get { return _name; }
        set
        {
            if (_name != value)
            {
                OnPropertyChanging("Name");
                _name = value;
                OnPropertyChanged("Name");
            }
        }
    }

    [Column(Storage = "_description", DbType = "varchar(200)", CanBeNull = true)]
    public virtual string Description
    {
        get { return _description; }
        set
        {
            if (_description != value)
            {
                OnPropertyChanging("Description");
                _description = value;
                OnPropertyChanged("Description");
            }
        }
    }
}

然后我定义了以下类

[MetadataType(typeof(ProductMetaData))]
public partial class T_MKT_Product
{
}

public class ProductMetaData
{
    [Display(Order = -1)]
    public virtual string Code { get; set; }

    [Display(Order = -2)]
    public object Name { get; set; }

    [Display(Order = -3)]
    public object Description { get; set; }
}

帮助! :)

I'm not being able to attach a MetadataType to a auto generated class in our application.
I tested setting the Order attribute in the generated class and it works fine, but if try to use another class i cannot get the attributes later.

I also already tried the solution suggested here with no success.

Generated class

[Table(Name = "T_MKT_Product")]
public partial class T_MKT_Product : GlobalSist.DataAccess.Base.BaseEntity
{
    [Column(Storage = "_code", DbType = "varchar(20)", IsUnique = true)]
    public virtual string Code
    {
        get { return _code; }
        set
        {
            if (_code != value)
            {
                OnPropertyChanging("Code");
                _code = value;
                OnPropertyChanged("Code");
            }
        }
    }

    [Column(Storage = "_name", DbType = "varchar(200)")]
    public virtual string Name
    {
        get { return _name; }
        set
        {
            if (_name != value)
            {
                OnPropertyChanging("Name");
                _name = value;
                OnPropertyChanged("Name");
            }
        }
    }

    [Column(Storage = "_description", DbType = "varchar(200)", CanBeNull = true)]
    public virtual string Description
    {
        get { return _description; }
        set
        {
            if (_description != value)
            {
                OnPropertyChanging("Description");
                _description = value;
                OnPropertyChanged("Description");
            }
        }
    }
}

Then i defined the following classes

[MetadataType(typeof(ProductMetaData))]
public partial class T_MKT_Product
{
}

public class ProductMetaData
{
    [Display(Order = -1)]
    public virtual string Code { get; set; }

    [Display(Order = -2)]
    public object Name { get; set; }

    [Display(Order = -3)]
    public object Description { get; set; }
}

Help! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰雪梦之恋 2025-01-02 12:46:54

Attribute.IsDefined(currentProp, typeof(DisplayAttribute)) 仍然是 False
这一定是假的。也许您正在使用自定义代码检查订单!您的问题的答案非常简单:您的自定义代码根本就是错误的。可能这是错误的,因为您假装找到随元数据类添加的属性以及该类的所有其他“本机”属性。这是错误的! .Net Clr 没有对 MetaDataType 属性的原生支持!这只是一个惯例。由您决定,验证您的类是否具有 MetaDataType,并检索与原始类的属性名称相同的 MetaDataType 的属性。
我的意思是你必须手动完成这项工作。 Mvc 引擎自动处理的所有属性都以这种方式处理...即 Mvc 引擎查看 MetaDataType 的属性并将它们与本机属性合并...您必须在自定义代码中执行相同的操作。

也就是说,如果您需要在视图中使用属性...而不是手动检索属性,请编写自定义 MetaDataProvider。元数据提供程序逻辑自动为您检索所有属性(按照我解释的方式)...您只需指定对每个属性采取什么操作。

Attribute.IsDefined(currentProp, typeof(DisplayAttribute))is still False
IT MUST BE FALSE. Probably you are checking the Order with a custom code! The answer to your problem is very easy: your custom code is simply WRONG. Probably it is wrong because you pretend to find the attribute added with the methadata class together with all other "native" attributes of the class. THIS IS WRONG! .Net Clr HAS NO NATIVE SUPPORT FOR MetaDataType Attribute! IT IS JUST A CONVENTION. It is up to you, verifyng that your class has a MetaDataType and retrieving also the attributes of the MetaDataType with the same name of the properties of your original class.
I MEAN YOU HAVE TO DO THIS JOB MANUALLY. All Attributes that the Mvc engine handles automatically are handled this way...that is the Mvc Engine look at the attributes of the MetaDataType and merge them with the native attributes...You have to do the same in your custom code.

That said if you need your attribute in a view...instead of retrieving manually your attributes, write a custom MetaDataProvider. The metadata provider logics automatically retrieve for you all attributes(the way I explained)...you need just to specify what action to take for each of them.

就像说晚安 2025-01-02 12:46:54

确保定义自动生成的类的命名空间与定义自定义分部类的命名空间相同。例如:

namespace FooBar
{
    [Table(Name = "T_MKT_Product")]
    public partial class T_MKT_Product : GlobalSist.DataAccess.Base.BaseEntity
    {
        ...
    }
}

还有你的:

namespace FooBar
{
    [MetadataType(typeof(ProductMetaData))]
    public partial class T_MKT_Product
    {
    }

    public class ProductMetaData
    {
        [Display(Order = -1)]
        public virtual string Code { get; set; }

        [Display(Order = -2)]
        public object Name { get; set; }

        [Display(Order = -3)]
        public object Description { get; set; }
    }
}

Make sure that the namespace into which is defined your autogenerated class is the same as the one in which you defined your custom partial class. For example:

namespace FooBar
{
    [Table(Name = "T_MKT_Product")]
    public partial class T_MKT_Product : GlobalSist.DataAccess.Base.BaseEntity
    {
        ...
    }
}

and yours:

namespace FooBar
{
    [MetadataType(typeof(ProductMetaData))]
    public partial class T_MKT_Product
    {
    }

    public class ProductMetaData
    {
        [Display(Order = -1)]
        public virtual string Code { get; set; }

        [Display(Order = -2)]
        public object Name { get; set; }

        [Display(Order = -3)]
        public object Description { get; set; }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文