How to: Work with Multiple Data Sources

This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.

This topic will show you how to work with multiple data sources (multiple .RLINQ files) in the same project. For the purpose of this tutorial, the following databases will be used:

  • SofiaCarRental
  • Northwind

To use multiple data sources:

  1. Open Visual Studio and create a new Class Library project.
  2. Right-click on your project, point Add and then New Item.
  3. In the Add New Item dialog, select Telerik Data Access Domain Model and name it SofiaCarRentalDomainModel.
  4. The Telerik Data Access New Domain Model Wizard starts. In the Choose Domain Model Type Dialog, select Populate from database.
  5. Set the "Use Custom defined namespace" option to SofiaCarRentalNamespace. Set SofiaCarRentalDbContext for Model name.

    When you have two or more domain models in the same project and those domain models expose the same entity (e.g. Customer), but are from different databases, then Telerik Data Access has to distinguish them somehow. One possible solution in this case is to separate the duplicated entities in different namespaces.

  6. Click Next to continue.

  7. In the Setup Database Connection dialog specify a connection string to the SofiaCarRental database. Also you need to specify a unique connection string name. Click Next to continue.
  8. In the Choose Database Items Dialog, include all tables. Click Next to continue.
  9. Skip the Define Naming Rules Dialog and click Next to proceed.
  10. In the Advanced Options Dialog, set the "Generate multiple files" option to False.

  11. Click Finish to generate the model.

To consume data from both of the domain models:

  1. Add a new WPF project to the solution.
  2. In your WPF project, add a reference to the class library project. Also add a reference to the following Telerik Data Access assemblies:
    1. Telerik.OpenAccess.dll
    2. Telerik.OpenAccess.35.Extensions.dll
  3. Copy and paste the App.config file from the class library project to the WPF project.
  4. Open the MainWindow.xaml file. Replace the existing XAML with the following one:

    <Window x:Class="DomainModelsDemo.MainWindow"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           Title="MainWindow">
       <Grid Margin="11" >
           <Grid.RowDefinitions>
               <RowDefinition />
               <RowDefinition />
           </Grid.RowDefinitions>
           <DataGrid x:Name="gridSofiaCarRental" />
           <DataGrid x:Name="gridNorthiwnd" Grid.Row="1" />
       </Grid>
    </Window>
    
  5. Switch to the code-behind and add the following code:

    public partial class MainWindow : Window
    {
       private SofiaCarRentalNamespace.SofiaCarRentalDbContext sofiaContext = 
           new SofiaCarRentalNamespace.SofiaCarRentalDbContext();
       private NorthwindNamespace.NorthwindDbContext northwindContext = 
           new NorthwindNamespace.NorthwindDbContext();
       public MainWindow()
       {
           InitializeComponent();
           this.gridNorthiwnd.ItemsSource = northwindContext.Customers.ToList();
           this.gridSofiaCarRental.ItemsSource = sofiaContext.Customers.ToList();
       }
    }
    
    Partial Public Class MainWindow
     Inherits Window
     Private sofiaContext As New SofiaCarRentalNamespace.SofiaCarRentalDbContext()
     Private northwindContext As New NorthwindNamespace.NorthwindDbContext()
     Public Sub New()
      InitializeComponent()
      Me.gridNorthiwnd.ItemsSource = northwindContext.Customers.ToList()
      Me.gridSofiaCarRental.ItemsSource = sofiaContext.Customers.ToList()
     End Sub
    End Class
    
  6. Build and run the demo application.

In the last demo all Customers from both of the data sources (domain models) were loaded and displayed in DataGrid controls. It is important to remember that when you have two or more domain models in the same project and those domain models expose the same entity (e.g. Customer), but are from different databases, then you have to distinguish them somehow. You have the following possible solutions:

  • You should separate each entity in a different namespace. In order to do that you have to use the option defined in the Advanced Options dialog of the Telerik Data Access Create Model Wizard.

    -or-

  • You should rename one of the duplicated entities in the Visual Designer.