If you need to create a LinqConnect model (context and entity classes) that can be used with different databases (for example, SQL Server and SQLite), having similar database objects, such scenario can be implemented in the following way.
Each DataContext instance should be provided with the separate mapping information for the database, that it will work with. Thus, you can create XML mapping files for each database separately, and at run-time you can provide these mapping files together with the corresponding connection strings when creating DataContext instances.
You can generate XML mapping using either our default LinqConnect template, generating strongly typed context and persistence aware classes, or POCO template, that generates persistence ignorant entity classes and XML mapping.
To generate XML mapping for the databases, perform the following steps:
- To add a template to your model (if a necessary template is not already added), right-click the Templates node in the Model Explorer window and select Add Template from the shortcut menu. Select the required template in the opened dialog box and click OK.
- If you need to remove unnecessary templates, select them in the Model Explorer window and press the DELETE key, then click OK.
- For LinqConnect template you need to enable File mapping mode. Double-click the empty space on a diagram, then in the Mapping Mode drop-down list select File. For POCO template, you may skip this step.
- Save the model to generate the code and mapping. The mapping XML file is generated in the same folder as your model, and has the same name as the model file, and 'xml' extension.
- Copy this file to some place and rename it.
- Right-click the database connection in the Database Explorer window and select Edit Connection Properties from the shortcut menu.
- In the Provider drop-down list, select the required provider.
- Specify the parameters of the connection to the database server, and click OK.
- Right-click the diagram and select Regenerate Storage and Mapping from the shortcut menu.
- Save the model to generate the code and mapping.
- Copy the new XML file with the mapping for the new database and rename it so that it will not be overwritten.
- Repeat the steps 5-10 for another database if necessary, until you create mapping files for all needed databases.
After this, you may create your context instances and use them with your databases like in the code sample above.