LightSwitch is primarily targeted at developers who need to rapidly product business applications.
Visual Studio LightSwitch application could connect to a variety of data sources. For now it can connect to database servers, SharePoint Lists and WCF RIA Services .
Connection with WCF RIA Services requires some settings that are not "out of the box". Despite this it is a very often expected scenario and developers want to have a sample where step by step to create a LightSwitch application, using as data source WCF RIA Services. That is the reason to create this walkthrough.
Demo Application:
Requirements:
Steps to implement the application:
- Create a sample database (HYPO database)
- Create a WCF RIA Services Class Library
- Add an ADO.NET Entity Data Model
- Add a Domain Service Class
- Create a Visual Studio LightSwitch application
- Add a WCF RIA Services data source
- Create a screen, using a WCF RIA Services data source
Steps to Reproduce:
Create a database, named HYPO
Set a fields in the table “hippos”.
- Create a WCF RIA Services Class Library, named RIAServicesLibrary1
- Add an ADO.NET Entity Data Model
Delete generated Class1 and add an ADO.NET Entity Data Model using data from database “HYPO”.
- Add a Domain Service Class
Create a Domain Service Class, named HippoDomainService using the created ADO.NET Entity Data Model (HYPOEntityModel).
LightSwitch applications require WCF RIA Data Source to has default query and key for entities.
Modify HippoDomainService class in HippoDomainService.cs : add an attribute [Query(IsDefault=true)] to the method HippoDomainService.GetHippos().
1: [Query(IsDefault=true)]
2: public IQueryable GetHippos()
3: {
4: return this.ObjectContext.hippos;
5: }
Add an attribute [Key] to the ID field in the hippos class (HippoDomainService.metadata.cs)
1: internal sealed class hipposMetadata
2: {
3:
4: // Metadata classes are not meant to be instantiated.
5: private hipposMetadata()
6: {
7: }
8:
9: public Nullable<int> Age { get; set; }
10:
11: [Key]
12: public long ID { get; set; }
13:
14: public string NAME { get; set; }
15:
16: public string Region { get; set; }
17:
18: public Nullable<decimal> Weight { get; set; }
19: }
- Create a Visual Studio LightSwitch application
Add a new LightSwitch application (C#) to the solution with WCF RIA Class Library:
- Add a WCF RIA Services data source
Add a reference to RIAServicesLibrary1.Web and add HippoDomainService as data source.
Add the entity “hippos” as a data source object:
Ensure “hippos” properties:
Switch view type for the LightSwitch application to “File View” and display hidden files.
Open in RIAServicesLibrary1.Web App.config file and copy the connection string to Web.config in a LightSwitch ServerGenerated project.
Connection string:
1: <add name="HYPOEntities" connectionString="metadata=res://*/HYPOEntityModel.csdl|res://*/HYPOEntityModel.ssdl|res://*/HYPOEntityModel.msl; providerName="System.Data.EntityClient" /
2: provider=System.Data.SqlClient;
3: provider connection string="
4: Data Source=.\SQLEXPRESS;Initial Catalog=HYPO;Integrated Security=True;
5: MultipleActiveResultSets=True""
6: >connectionStrings>
- Create a screen, using a WCF RIA Services data source
Add a new Search Data Screen, named Searchhippos using hippos entity:
Ensure screen properties:
Run the application: hippos data is displayed properly.
Modify hippos data.
Source code of the demo application you could download here: LSRIADemo.zip