views:

1152

answers:

2

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, which is being used for the SQL generated by LINQ. In the .dbml file it looks like this:

<Table Name="dbo.Customers" Member="Customers">

Now I'd like to change this table name during runtime, so the SQL will be run against some other table (customers2008 instead of customer, for example).

Is there any way to change the table name (source name) during runtime?

[Update] After some testing to my dismay I had to discover that XmlMappingSource does render computed properties which are not persisted in the database unaccessable (yes, even the mapping created by SqlMetal does ignore everything which is not persisted).

+2  A: 

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.

jrummell
Now I only need a way to create this mapping files during runtime, so I won't have to try and keep the mapping file in alignment with the data classes.
Sam
Oh well, I managed to create these files, but alas, they do not work as expected. Running out of time now, more tomorrow.
Sam
I would recommend creating the xml mapping in the post-build event instead of at runtime.
jrummell
Even the mapping files created with SqlMetal do render the DataContext unusable, since they disable access to computed properties which are not persisted in the database. Pity.
Sam
Did you add these properties in a partial class? If so, they should still be accessible. You wouldn't be able to use them in a query, however, because they don't map to a column in the database. This is true for xml and attribute based mapping.
jrummell
+2  A: 

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.
David B
You don't happen to have a link or some source how to create a mapping file in a running program?
Sam