附加元数据类ADO.NET实体数据模型类 [英] Attaching MetaData class to ADO.NET Entity Data model classes

查看:121
本文介绍了附加元数据类ADO.NET实体数据模型类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有附加的元数据类ADO.NET实体数据模型生成的类的问题。 根据下面的链接...

<一个href="http://blogs.microsoft.co.il/blogs/gilf/archive/2011/01/20/adding-metadata-to-entities-in-the-data-model.aspx" rel="nofollow">http://blogs.microsoft.co.il/blogs/gilf/archive/2011/01/20/adding-metadata-to-entities-in-the-data-model.aspx

http://msdn.microsoft.com/en-us/library/ cc679243.aspx

<一个href="http://goneale.com/2009/03/04/using-metadatatype-attribute-with-aspnet-mvc-xval-validation-framework/" rel="nofollow">http://goneale.com/2009/03/04/using-metadatatype-attribute-with-aspnet-mvc-xval-validation-framework/

<一个href="http://davidhayden.com/blog/dave/archive/2008/01/06/ASPNETDynamicDataTutorialBuddyMetadataProviderCustomMetadataProviders.aspx" rel="nofollow">http://davidhayden.com/blog/dave/archive/2008/01/06/ASPNETDynamicDataTutorialBuddyMetadataProviderCustomMetadataProviders.aspx

<一个href="http://davidhayden.com/blog/dave/archive/2008/05/15/DynamicDataWebsitesScaffoldTableScaffoldColumnAttributes.aspx" rel="nofollow">http://davidhayden.com/blog/dave/archive/2008/05/15/DynamicDataWebsitesScaffoldTableScaffoldColumnAttributes.aspx

我创建了一个元数据类的一些属性添加到属性。我可以添加这个属性的属性生成的类和它的作品,但我想为避免失去这个属性,我每次都更新并重新创建我的ADO.NET实体数据模型。

我的问题是,我究竟做错了什么?为什么运行属性没有我的自定义属性?

这是生成的数据类的一部分

  [EdmEntityTypeAttribute(NamespaceName =HelpMeHowModel,名称=文章)
[序列化()]
[DataContractAttribute(IsReference =真)
[MetadataType(typeof运算(ArticleMetaData))]
公共部分类文章:EntityObject
{
    #地区的原始属性

    ///&LT;总结&gt;
    ///没有元数据文档。
    ///&LT; /总结&gt;
    [EdmScalarPropertyAttribute(EntityKeyProperty =假,ISNULLABLE = FALSE)]
    [DataMemberAttribute()]
    大众全球::可选System.Boolean |评论
    {
        得到
        {
            返回_IsPublished;
        }
        组
        {
            OnIsPublishedChanging(值);
            ReportPropertyChanging(|评论);
            _IsPublished = StructuralObject.SetValidValue(值);
            ReportPropertyChanged(|评论);
            OnIsPublishedChanged();
        }
    }
    私人全球::可选System.Boolean _IsPublished;
    部分无效OnIsPublishedChanging(全球::可选System.Boolean值);
    部分无效OnIsPublishedChanged();
 

...

..这是我的元数据类

 公共类ArticleMetaData
{
    #地区的原始属性

    [BoolFunction(BoolFunction.ThreeStateRadioButton)
    大众全球::可选System.Boolean |评论{获得;组; }
 

解决方案

有关每个人都在寻找对同一问题的解决方案...

添加自定义属性部分MetadataType类是可能的,它的工作原理,但有一个小问题。

使用

 的PropertyInfo PI;

pi.GetCustomAttributes(...)
 

会得到,而不是只在主类的属性,从作为MetadataType类。

根据这里的解决方案解释

<一个href="http://stackoverflow.com/questions/1910532/attribute-isdefined-doesnt-see-attributes-applied-with-metadatatype-class">Attribute.IsDefined没有看到施加MetadataType类属性

我创造的PropertyInfo类中的两个扩展方法来获得所有属性。

 命名空间FAIN.Framework.Commons
{
    公共静态类PropertyInfoEx
    {
        公共静态对象[] GetAllCustomAttributes(此的PropertyInfo PI,布尔继承)
        {
            返回GetAllCustomAttributes(PI,空,继承);
        }
        ///&LT;总结&gt;
        ///获取自定义属性在MetadataType添加+属性
        ///&LT; /总结&gt;
        ///&LT; PARAM NAME =圆周率&GT;&LT; /参数&GT;
        ///&LT; PARAM NAME =属性类型&GT;&LT; /参数&GT;
        ///&LT; PARAM NAME =继承&GT;&LT; /参数&GT;
        ///&LT;返回&GT;&LT; /回报&GT;
        公共静态对象[] GetAllCustomAttributes(此的PropertyInfo PI,类型属性类型,布尔继承)
        {
            如果(PI == NULL)返回NULL;
            名单&LT;对象&gt; allAttributes =新的名单,其中,对象&gt;();
            [对象]属性= NULL;
            如果(属性类型!= NULL)
            {
                属性= pi.GetCustomAttributes(属性类型,继承);
            }
            其他
            {
                属性= pi.GetCustomAttributes(继承);
            }
            allAttributes.AddRange(属性);

            //搜索类声明的属性的所有元数据来获取所有CustomAttributes如果有任何
            MetadataTypeAttribute [] metadataTypes = pi.DeclaringType.GetCustomAttributes(typeof运算(MetadataTypeAttribute),真).OfType&LT; MetadataTypeAttribute&GT;()的ToArray();
            的foreach(MetadataTypeAttribute元数据metadataTypes)
            {

                如果(元!= NULL)
                {
                    的PropertyInfo []属性= metadata.MetadataClassType.GetProperties();
                    的PropertyInfo的PropertyInfo = properties.Where(P =&GT; p.Name == pi.Name).FirstOrDefault();
                    如果(的PropertyInfo!= NULL)
                    {
                        如果(属性类型!= NULL)
                        {
                            属性= propertyInfo.GetCustomAttributes(属性类型,继承);
                        }
                        其他
                        {
                            属性= propertyInfo.GetCustomAttributes(继承);
                        }
                        allAttributes.AddRange(属性);
                    }
                }
            }

            返回allAttributes.ToArray();
        }
    }
}
 

I have a problem attaching metadata class to ADO.NET entity data model generated classes. According to the following links...

http://blogs.microsoft.co.il/blogs/gilf/archive/2011/01/20/adding-metadata-to-entities-in-the-data-model.aspx

http://msdn.microsoft.com/en-us/library/cc679243.aspx

http://goneale.com/2009/03/04/using-metadatatype-attribute-with-aspnet-mvc-xval-validation-framework/

http://davidhayden.com/blog/dave/archive/2008/01/06/ASPNETDynamicDataTutorialBuddyMetadataProviderCustomMetadataProviders.aspx

http://davidhayden.com/blog/dave/archive/2008/05/15/DynamicDataWebsitesScaffoldTableScaffoldColumnAttributes.aspx

I created a metadata class to add some Attributes to properties. I could add this attributes to properties in generated classes and It works but I wanted to avoid loosing this attributes every time I have to update and recreate my ADO.NET entity data model.

My question is, what am I doing wrong ? Why in runtime properties does not have my custom attributes ?

This is a part of generated data class

[EdmEntityTypeAttribute(NamespaceName="HelpMeHowModel", Name="Article")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
[MetadataType(typeof(ArticleMetaData))]
public partial class Article : EntityObject
{
    #region Primitive Properties

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.Boolean IsPublished
    {
        get
        {
            return _IsPublished;
        }
        set
        {
            OnIsPublishedChanging(value);
            ReportPropertyChanging("IsPublished");
            _IsPublished = StructuralObject.SetValidValue(value);
            ReportPropertyChanged("IsPublished");
            OnIsPublishedChanged();
        }
    }
    private global::System.Boolean _IsPublished;
    partial void OnIsPublishedChanging(global::System.Boolean value);
    partial void OnIsPublishedChanged();

...

.. and this is my metadata class

public class ArticleMetaData
{
    #region Primitive Properties

    [BoolFunction(BoolFunction.ThreeStateRadioButton)]
    public global::System.Boolean IsPublished { get; set; }

解决方案

For everybody looking for the solution for the same problem...

Adding custom attributes to partial MetadataType class is possible and it works but there is a little problem.

Using

PropertyInfo pi;

pi.GetCustomAttributes(...) 

will get the Attributes from the main class only and not from the class used as MetadataType.

Based on solution explained here

Attribute.IsDefined doesn't see attributes applied with MetadataType class

I created two extension methods for PropertyInfo class to get all attributes.

namespace FAIN.Framework.Commons
{
    public static class PropertyInfoEx
    {
        public static object[] GetAllCustomAttributes(this PropertyInfo pi, bool inherit)
        {
            return GetAllCustomAttributes(pi, null, inherit);
        }
        /// <summary>
        /// Get Custom Attributes + attributes added in MetadataType
        /// </summary>
        /// <param name="pi"></param>
        /// <param name="attributeType"></param>
        /// <param name="inherit"></param>
        /// <returns></returns>
        public static object[] GetAllCustomAttributes(this PropertyInfo pi, Type attributeType, bool inherit)
        {
            if (pi == null) return null;
            List<object> allAttributes = new List<object>();
            object[] attributes = null;
            if (attributeType != null)
            {
                attributes = pi.GetCustomAttributes(attributeType, inherit);
            }
            else
            {
                attributes = pi.GetCustomAttributes(inherit);
            }
            allAttributes.AddRange(attributes);

            // search all the Metadata of the class declaring the property to get all CustomAttributes if there are any
            MetadataTypeAttribute[] metadataTypes = pi.DeclaringType.GetCustomAttributes(typeof(MetadataTypeAttribute), true).OfType<MetadataTypeAttribute>().ToArray();
            foreach (MetadataTypeAttribute metadata in metadataTypes)
            {

                if (metadata != null)
                {
                    PropertyInfo[] properties = metadata.MetadataClassType.GetProperties();
                    PropertyInfo propertyInfo = properties.Where(p => p.Name == pi.Name).FirstOrDefault();
                    if (propertyInfo != null)
                    {
                        if (attributeType != null)
                        {
                            attributes = propertyInfo.GetCustomAttributes(attributeType, inherit);
                        }
                        else
                        {
                            attributes = propertyInfo.GetCustomAttributes(inherit);
                        }
                        allAttributes.AddRange(attributes);
                    }
                }
            }

            return allAttributes.ToArray();
        }
    }
}

这篇关于附加元数据类ADO.NET实体数据模型类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆