Asp.net 将元数据类附加到ADO.NET实体数据模型类
将元数据类附加到ADO.NET实体数据模型生成的类时出现问题。 根据以下链接 我创建了一个元数据类来向属性添加一些属性。我可以将此属性添加到生成的类中的属性中,并且可以正常工作,但我希望避免每次必须更新和重新创建ADO.NET实体数据模型时丢失此属性。 我的问题是,我做错了什么?为什么运行时属性没有我的自定义属性? 这是生成的数据类的一部分Asp.net 将元数据类附加到ADO.NET实体数据模型类,asp.net,ado.net,entity,metadata,Asp.net,Ado.net,Entity,Metadata,将元数据类附加到ADO.NET实体数据模型生成的类时出现问题。 根据以下链接 我创建了一个元数据类来向属性添加一些属性。我可以将此属性添加到生成的类中的属性中,并且可以正常工作,但我希望避免每次必须更新和重新创建ADO.NET实体数据模型时丢失此属性。 我的问题是,我做错了什么?为什么运行时属性没有我的自定义属性? 这是生成的数据类的一部分 [EdmEntityTypeAttribute(NamespaceName="HelpMeHowModel", Name="Article")]
[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类添加自定义属性是可能的,它可以工作,但有一个小问题 使用 将仅从主类获取属性,而不是从用作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();
}
}
}
namespace FAIN.Framework.Commons
{
公共静态类PropertyInfoEx
{
公共静态对象[]GetAllCustomAttributes(此属性信息pi,布尔继承)
{
返回GetAllCustomAttributes(pi,null,inherit);
}
///
///获取自定义属性+在MetadataType中添加的属性
///
///
///
///
///
公共静态对象[]GetAllCustomAttributes(此属性信息为pi,类型为attributeType,布尔继承)
{
如果(pi==null)返回null;
List allAttributes=新列表();
object[]attributes=null;
if(attributeType!=null)
{
attributes=pi.GetCustomAttributes(attributeType,inherit);
}
其他的
{
attributes=pi.GetCustomAttributes(继承);
}
allAttributes.AddRange(属性);
//搜索声明属性的类的所有元数据以获取所有CustomAttributes(如果有)
MetadataTypeAttribute[]metadataTypes=pi.DeclaringType.GetCustomAttributes(typeof(MetadataTypeAttribute),true).OfType().ToArray();
foreach(MetadataTypeAttribute元数据在metadataTypes中)
{
if(元数据!=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);
}
其他的
{
attributes=propertyInfo.GetCustomAttributes(继承);
}
allAttributes.AddRange(属性);
}
}
}
返回allAttributes.ToArray();
}
}
}
我忘了说我正在开发一个常规的ASP.NET应用程序!看看这个,我已经回答了这个问题
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();
}
}
}