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

前端 未结 1 1701
难免孤独
难免孤独 2021-01-28 04:24

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 w

相关标签:
1条回答
  • 2021-01-28 05:05

    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";
    }
    
    0 讨论(0)
提交回复
热议问题