In this article you will learn how to make WCF RIA Service and how to consume that using Visual Studio LightSwitch 2012.
What is WCF RIA Service
RIA services is a server-side technology that automatically generates client-side (Silverlight) objects that take care of the communication with the server for you and provide client-side validation. The main object inside a RIA service is a DomainService, usually a LinqToEntitiesDomainService that is connected to a LinqToEntities model.
- Open Visual Studio 2012.
- Go to "File" => "New" => "Project..."
- Select "LightSwitch" in installed templates.
- Select "LightSwitch Application (Visual C#)".
- Enter the Name and choose the location.
- Click "OK".
First of all let's create a new Class Library, Right click on solution and Add new Project.
Now add a new ADO.NET Entity Data Model using right click and click add new item.
Choose Model Contents and click next.
Choose your data connection.
Now choose your database objects and settings and click finish.
Now right click on class library and click Add New Item and select Domain Service Class and edit the name and click Add button.
If you don't see your context class in drop down, then read this:
With Visual Studio 2012, Entity Framework now creates 'DbContext' based context classes by default, but WCF RIA Services does not support 'DbContext' based context classes. In order to utilize your Entity Framework model with WCF RIA Services, you must convert it to an 'ObjectContext' based model. This can be done using the following steps:
1. Open your entity model in the designer, change the "Code Generation Strategy" from "None" to "Default"
2. Delete the two ".tt" files that are adjacent to the model
3. Rebuild the project
After following those steps, you will be able to select your entity model's context class in the 'Available context classes' list. The side-effect of this procedure is that you have now converted your entity model from an Entity Framework DbContext-based model to an ObjectContext-based model. Now again add a new Domain Service Class and click ok.
As you can see your data context class in drop and uncheck Enable client access and select entity and check enable editing if you want to edit and click OK.
Now open domain service class and add Isdefault.
// Consider constraining the results of your query method. If you need additional input you can
// add parameters to this method or create additional query methods with different names.
// To support paging you will need to add ordering to the 'Customers' query.
public IQueryable < Customer > GetCustomers()
return this .ObjectContext.Customers ;
Now let's start work on data source and screens. Add an new Data Source.
Select WCF RIA Service and click Next.
Now click on Add Reference and click next.
Select Projects tab and do check class library assembly and click OK.
Note - if you are using framework 4.5 then may be assembly reference won't display so you have to change framework 4.5. To 4.0 in class library.
Now select Entities and click Finish.
Solution structure will look like this.
Now open App.Config and copy connection string key.
metadata=res://*/CustomerModel.csdl|res://*/CustomerModel.ssdl|res://*/CustomerModel.msl;provider=System.Data.SqlClient;provider connection string=
source=RAJ-PC;initial catalog=NORTHWND;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework
Now let's work on screens.
Add a new Editable Grid Screen and Screen data drop down list and click OK.
Finally time to run the project.
Click on Add Icon.
Click on Edit Icon.