如何添加属性级别的属性在运行时映射ColumnAttribute? [英] How to add property-level Attribute to map ColumnAttribute at runtime?
问题描述
从这个<一个href=\"http://stackoverflow.com/questions/12143650/how-to-add-property-level-attribute-to-the-typedescriptor-at-runtime/\">question我们已实施相应的属性的ColumnAttribute关联,但是当LINQ到SQL试图将此属性映射到列这是行不通的。
From this question we have implemented the ColumnAttribute association with the corresponding property, but when Linq to SQL tries to map this property to a Column it doesn't work.
我们的财产和映射code(从另一个问题):
Our property and mapping code(from the other question):
public System.Xml.Linq.XElement Name {
get {
return this.name;
}
set {
this.OnNameChanging(value);
this.SendPropertyChanging();
this.name = value;
this.SendPropertyChanged("Name");
this.OnNameChanged();
}
}
System.Data.Linq.Mapping.ColumnAttribute columnAttribute = new System.Data.Linq.Mapping.ColumnAttribute();
columnAttribute.Name = "Name";
columnAttribute.Storage = "name";
columnAttribute.DbType = "Xml NOT NULL";
columnAttribute.CanBeNull = false;
columnAttribute.UpdateCheck = System.Data.Linq.Mapping.UpdateCheck.Never;
PropertyOverridingTypeDescriptor propertyOverrideTypeDescriptor = new PropertyOverridingTypeDescriptor(TypeDescriptor.GetProvider(typeof(ClassToMap)).GetTypeDescriptor(typeof(ClassToMap)));
PropertyDescriptor pd = TypeDescriptor.GetProperties(typeof(ClassToMap)).Cast<PropertyDescriptor>().ToArray().Where(prop => prop.Name == "Name").FirstOrDefault();
PropertyDescriptor pd2 = TypeDescriptor.CreateProperty(
typeof(ClassToMap).GetType(),
pd, // base property descriptor to which we want to add attributes
// The PropertyDescriptor which we'll get will just wrap that
// base one returning attributes we need.
columnAttribute
// this method really can take as many attributes as you like, not just one
);
propertyOverrideTypeDescriptor.OverrideProperty(pd2);
TypeDescriptor.AddProvider(new TypeDescriptorOverridingProvider(typeof(ClassToMap)), typeof(ClassToMap));
不知道如何刷新表映射?
Any idea how to refresh the table mapping?
推荐答案
反射不使用类型描述符。您的测试很可能获得通过,因为另一个属性添加到code类。
Reflection does not utilize type descriptors. Your test probably passed because another attribute is added to a class in code.
有就是改变的LINQ to SQL类映射在飞行的方法。诀窍是一个XML映射文件添加到资源,当你创建一个数据上下文中使用它。当你的XML数据加载到内存中,可以根据您的需求决定修改映射。该解决方案在这里详细的描述:<一href=\"http://social.msdn.microsoft.com/Forums/en-US/f5e0af64-0057-4498-998b-70db656a0e9f/linq-to-sql-using-dynamic-tables\"相对=nofollow> LINQ到使用动态表的SQL
There is a way to change LINQ to SQL classes mapping on the fly. The trick is to add a XML mapping file to resources and use it when you create a data context. When you load XML data into memory, you can modify mappings as your needs dictate. This solution was described in detail here: Linq to sql using dynamic tables
在code片断:
foreach (XElement col in xe.Descendants().Where(e => e.Name.LocalName == "Column")) {
XAttribute name = col.Attributes().FirstOrDefault(a => a.Name.LocalName == "Name" && a.Value == "CategoryName");
if (name != null)
name.Value = "Name";
}
这篇关于如何添加属性级别的属性在运行时映射ColumnAttribute?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!