從這question我們已經實現了與相應屬性的ColumnAttribute關聯,但是當Linq to SQL嘗試將此屬性映射到Column時,它不起作用。如何添加屬性級別屬性以在運行時映射ColumnAttribute?
我們的財產和映射代碼(從其它問題):
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));
任何想法如何刷新表映射?
我已經試過這其他替代http://stackoverflow.com/questions/393687/how-can-i-add-該解決方案在這裏詳細描述了my-attributes-code-generated-linq2sql-classes-properties但仍然不起作用。 – lorddarkangel