How to add property-level Attribute to map ColumnAttribute at runtime?

老子叫甜甜 提交于 2019-12-02 10:11:51

Reflection does not utilize type descriptors. Your test probably passed because another attribute is added to a class in code.

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

The code snippet:

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";
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!