我在将元数据类附加到 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; }
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; }
发布评论
评论(1)
对于每个正在寻找同一问题解决方案的人来说...
向部分 MetadataType 类添加自定义属性是可能的,并且它可以工作,但有一个小问题。
使用
将仅从主类获取属性,而不是从用作 MetadataType 的类获取属性。
基于此处解释的解决方案
Attribute.IsDefined 没有看到应用的属性使用 MetadataType 类
我为 PropertyInfo 类创建了两个扩展方法来获取所有属性。
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
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.