2011-10-23 108 views
0

將元數據類附加到ADO.NET實體數據模型生成的類時遇到問題。 根據下面的鏈接...將元數據類附加到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; } 
+0

我忘了說我正在開發一個常規的ASP.NET應用程序! – Patrik

+0

檢查了這一點,我已經在這裏回答了這個問題 http://stackoverflow.com/a/24757520/3050647 – elia07

回答

2

爲大家尋找的解決方案同樣的問題...

將自定義屬性添加到partia MetadataType類是可能的,它可以工作,但有一點問題。

使用

PropertyInfo pi; 

pi.GetCustomAttributes(...) 

會得到來自作爲MetadataType類,而不是主類的屬性。

基於這裏的解決方案

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

解釋我創建的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(); 
     } 
    } 
} 
相關問題