LinqConnect Documentation
In This Topic
    How to Create and Use One Model with Different Databases
    In This Topic
    How to Create and Use One Model with Different Databases
    LinqConnect Documentation
    How to Create and Use One Model with Different Databases
    [email protected]

    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.


    TestDataContext sqlContext = new TestDataContext(connectionStringSql, mappingSourceSql);
    // Some work with the SQL Server database via the context
    TestDataContext sqliteContext = new TestDataContext(connectionStringSqlite, mappingSourceSqlite);
    // Some work with the SQLite database via the context
    
    
    Dim sqlContext As New TestDataContext(connectionStringSql, mappingSourceSql)
    ' Some work with the SQL Server database via the context
    Dim sqliteContext As New TestDataContext(connectionStringSqlite, mappingSourceSqlite)
    ' Some work with the SQLite database via the context
    
    

    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:

    1. 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.
    2. If you need to remove unnecessary templates, select them in the Model Explorer window and press the DELETE key, then click OK.
    3. 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.
    4. 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.
    5. Copy this file to some place and rename it.
    6. Right-click the database connection in the Database Explorer window and select Edit Connection Properties from the shortcut menu.
    7. In the Provider drop-down list, select the required provider.
    8. Specify the parameters of the connection to the database server, and click OK.
    9. Right-click the diagram and select Regenerate Storage and Mapping from the shortcut menu.
    10. Save the model to generate the code and mapping.
    11. Copy the new XML file with the mapping for the new database and rename it so that it will not be overwritten.
    12. 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.