How to change LINQ O/R-M table name/source during runtime?

前端 未结 2 683
刺人心
刺人心 2020-12-19 07:35

I\'ve got a database, and an entityset created by the O/R-Mapper, using all this with LINQ.

In the O/R-Mapper I need to enter a table name (source) for every table,

相关标签:
2条回答
  • 2020-12-19 08:15

    Kind of ... You'd have to do something like this:

    1. Create the entities in the designer.
    2. Use SqlMetal to generate an xml mapping file from the .dbml file generate by the designer.
    3. Update the table names in the xml file.
    4. Then in your code, use the DataContext constructor overload that accepts an XmlMappingSource created with the xml mapping file.

    Here's a blog post that explains this process step by step: http://weblogs.asp.net/guybarrette/archive/2008/07/23/linq-to-sql-dynamic-mapping.aspx

    I'm using a similar process with an ERP database that has table names like ttccom001xxx where xxx is the installation ID (I know it's a horrible schema, but there's nothing I can do about it). We have multiple installations, so I copy the initial xml mapping once for each installation and then replace xxx with the installation ID. I wrote a small console app that uses Regex to take care the replacements and added it as part of my build process.

    0 讨论(0)
  • 2020-12-19 08:16

    Here's a link to Microsoft's External Mapping Reference.

    External mapping overrides attribute-based mapping.

    Here's the essentials.

    1. Create some xml that represents your mappings.
    2. New up an XmlMappingSource and give it your xml.
    3. When you construct your DataContext, use a Constructor that accepts the XmlMappingSource, such as this one.
    0 讨论(0)
提交回复
热议问题