我有一个问题附加元数据类到ADO。. NET实体数据模型生成类。根据以下链接…
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我创建了一个元数据类来向属性添加一些属性。我可以将此属性添加到生成类的属性中,它可以工作,但我想避免每次我必须更新和重新创建我的ADO时丢失此属性。. NET实体数据模型
我的问题是,我做错了什么?为什么在运行时属性没有我的自定义属性?
这是生成数据类
的一部分[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();
…
. .这是元数据类
public class ArticleMetaData
{
#region Primitive Properties
[BoolFunction(BoolFunction.ThreeStateRadioButton)]
public global::System.Boolean IsPublished { get; set; }
每个人都在为同样的问题寻找解决方案…
添加自定义属性到部分MetadataType类是可能的,它工作,但有一个小问题。
使用PropertyInfo pi;
pi.GetCustomAttributes(...)
将只从主类获取属性,而不从用作元数据类型的类获取属性。
基于这里解释的解决方案
属性。IsDefined没有看到应用于MetadataType类的属性
我为PropertyInfo类创建了两个扩展方法来获取所有属性。
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();
}
}
}