Data Access has been discontinued. Please refer to this page for more information.

Visual Studio LightSwitch with WCF RIA

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.

In this walkthrough you will learn how to create a Silverlight application with WCF RIA Services and Telerik Data Access, using many of the features of Visual Studio 2012.

Create a Project

The first step in creating a LightSwitch application is to create a new project:

  1. Open Visual Studio 2012, on the File menu, click New Project. The New Project dialog box appears.
  2. In the Installed Templates list, select the LightSwitch node.
  3. Select either LightSwitchApplication (VB) or LightSwitchApplication (C#).
  4. In the Name field, type SofiaCarRentalDemo and then click OK.

  5. The SofiaCarRentalDemo Designer appears, and the necessary folders for your project are added to the Solution Explorer. Close the designer.

Create WCF RIA Class Library

The next step is to create a reusable WCF RIA Class Library. For that purpose:

  1. On the File menu, point Add, and then click New Project.
  2. In the Installed Templates list, select the Silverlight node.
  3. Change the .NET Framework version from 4.5 to 4.0.
  4. Select WCF RIA Services Class Library. This will add a new project for creating a WCF RIA Services class library that can be consumed by a Silverlight application.
  5. In the Name field, type SofiaCarRentalWCFRIA and then click OK.

  6. The Solution Explorer should have the following structure:

  7. In the SofiaCarRentalWCFRIA.Web project add a new Telerik Data Access Domain Model. Name the model SofiaCarRentalDomainModel. In the Create Model Wizard, perform the following steps:

    1. In the Select Domain Model Type screen, choose Populate from database. Enter SofiaCarRentalDbContext for Model name.
    2. In the Setup Database Connection screen, create a data connection to the SofiaCarRental database and click Next.
    3. In the Choose Database Items screen, select all the tables.
    4. Click Finish. Persistent classes are generated for the tables.

  8. Build your solution.

  9. In the SofiaCarRentalWCFRIA.Web project add a new Telerik Data Access Domain Service. Name it SofiaCarRentalDomainService.tt. The Telerik RIA Wizard will start.
    1. In the Select Metadata Source Type screen, select the Rlinq file option.
    2. Click Next to proceed to the Configure Domain Service Endpoints screen.
      1. In the Available metadata sources drop-down, select the SofiaCarRentalDomainModel.rlinq entry.
      2. Make sure that the Enable client access is checked.
      3. Check the Enable and Enable Edit settings for all entities exposed by the SofiaCarRentalDomainModel.rlinq.
      4. In the Domain service name textbox type SofiaCarRentalDomainService.
  10. Click Finish. The domain service class is generated.

  11. Open the SofiaCarRentalDomainService.generated.cs file. In order to use WCF RIA Service with LightSwitch application, one has to have default query methods in the service. This is done by attributing all GetXXX method with a Query attribute as follow:

    [EnableClientAccess()]
    public partial class SofiaCarRentalDomainService : OpenAccessDomainService<SofiaCarRentalDbContext>
    {
        [Query(IsDefault=true)]
        public IQueryable<RentalRate> GetRentalRates()
        {
         return this.DataContext.RentalRates;
        }
    }
    

    Although this will work for the current demo, there is one significant disadvantage. The SofiaCarRentalDomainService class is generated by a code-generation template. Each time, you save the template your manual changes made to the class will be lost. The best solution is to modify the template to add the Query attribute during the code generation. Open the SofiaCarRentalDomainService.tt file. Locate the following code fragment:

    public IQueryable<<#=classType#>> Get<<#=className#>()
    {
        return this.DataContext.<#=className#>;
    }
    

    And modify it, so it looks like:

    [Query(IsDefault=true)]
    public IQueryable<<#=classType#>> Get<#=className#>()
    {
        return this.DataContext.<#=className#>;
    }
    
  12. Build your solution again.

Create a new DataSource

The next step is to add a new data source to your LightSwitch project:

  1. In the Solution Explorer, select the SofiaCarRentalDemo project. Right-click on the DataSource folder and select Add Data Source.

  2. In the Attach Data Source Wizard dialog box, select WCF RIA Services and click Next to proceed.

  3. On the next page, click the Add Reference button. The Add Reference dialog box appears. Add a reference to the SofiaCarRentalWCFRIA.Web project.

  4. In the final page of the wizard, include all entities exposed by the SofiaCarRentalDomainService.

  5. Click Finish to add the data source.

  6. At this point your solution is not buildable. If you try to build your project now, you will receive several errors. You should perform several steps manual, in order to have buildable project.

  7. In the Solution Explorer, select the SofiaCarRentalDemo project and switch to the File View.

  8. Again in Solution Explorer, select the SofiaCarRentalDemo project and click the Show All Files option. Right now your solution explorer should look like the snapshot below:

  9. Expand the Server project and add reference Telerik.OpenAccess.dll, Telerik.OpenAccess.35.Extensions.dll and Telerik.OpenAccess.Ria.Extensions.dll assemblies.

  10. Build your project. Your project should be buildable.
  11. The final step is to copy and paste the Telerik Data Access connection string from the App.config file in the SofiaCarRentalWCFRIA.Web project to the Web.config file in the Server project.

    <connectionStrings>
      <add name="_IntrinsicData" 
           connectionString="Data Source=.\SQLEXPRESS;
                             AttachDbFilename=|DataDirectory|\DevelopmentDatabase.mdf;
                             Integrated Security=True;
                             Connect Timeout=30;
                             User Instance=True;
                             MultipleActiveResultSets=True" />
      <add name="SofiaCarRentalDbConnection" 
           connectionString="data source=.\SQLEXPRESS;
                             initial catalog=SofiaCarRental_v2.2;
                             integrated security=True" 
           providerName="System.Data.SqlClient" />
    </connectionStrings>
    
  12. Select the SofiaCarRentalDemo project in the Solution Explorer and switch to the Logical View.

Create a Search Screen

In the next step, you will create a screen for searching cars from the SofiaCarRental database:

  1. In the SofiaCarRentalDemo project click the Screen nodes, right-click and select Add Screen.
    The Add New Screen dialog box opens.

  2. In the Add New Screen dialog box, select Search Data Screen in the Select a screen template list.

  3. Select the Screen Data drop-down list and select Cars, and then click OK.

  4. The Screen Designer opens, displaying a hierarchical representation of the screen layout.

  5. Press F5 to start the application.