Introduction
Today, in this article let's play around with one of the interesting and most useful concepts in Silverlight.
Question: What is select data using Silverlight RIA enabled service?
In simple terms "This application enables selection of data in the database with help of a Silverlight RIA enabled service. It uses a base platform of Entity Framework to communicate with the database".
Step 1: Create a database named "Company" with employee table in it.
Step 2: Open up Visual Studio and create a new Silverlight application enabled with RIA Services
Step 3: When the project is created. Right-click on the RIA Service project and add a new entity data model framework and set it up for the previously created database.
Step 4: Again right-click on the RIA service project and add a Domain Service Class as a new item.
Step 5: The complete code of EmployeeDomain.cs looks like this (Domain Service Class):
namespace SilverlightRIASelectApp.Web
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq;
using System.ServiceModel.DomainServices.EntityFramework;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
// Implements application logic using the CompanyEntities context.
// TODO: Add your application logic to these methods or in additional methods.
// TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
// Also consider adding roles to restrict access as appropriate.
// [RequiresAuthentication][EnableClientAccess()]
public class EmployeeDomain : LinqToEntitiesDomainService<CompanyEntities>
{
// TODO:// 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 'Employee'
query.public IQueryable<Employee> GetEmployee()
{
return this.ObjectContext.Employee;
}
public void InsertEmployee(Employee employee)
{
if ((employee.EntityState != EntityState.Detached))
{
this.ObjectContext.ObjectStateManager.ChangeObjectState(employee, EntityState.Added);
}
else
{
this.ObjectContext.Employee.AddObject(employee);
}
}
public void UpdateEmployee(Employee currentEmployee)
{
this.ObjectContext.Employee.AttachAsModified(currentEmployee, this.ChangeSet.GetOriginal(currentEmployee));
}
public void DeleteEmployee(Employee employee)
{
if ((employee.EntityState != EntityState.Detached))
{
this.ObjectContext.ObjectStateManager.ChangeObjectState(employee, EntityState.Deleted);
}
else
{
this.ObjectContext.Employee.Attach(employee);
this.ObjectContext.Employee.DeleteObject(employee);
}
}
}
}
Step 6: Now rebuild the solution file.
Step 7: the Complete code of MainPage.xaml looks like this:
<usercontrolx:class="SilverlightRIASelectApp.MainPage"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"mc:ignorable="d"
d:designheight="300"d:designwidth="400"xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Gridx:Name="LayoutRoot"Background="White">
<sdk:DataGridHeight="216"HorizontalAlignment="Left"Margin="57,57,0,0"Name="dataGrid1"VerticalAlignment="Top"Width="254"/>
<ButtonContent="Show Data"Height="23"HorizontalAlignment="Left"Margin="158,14,0,0"Name="button1"VerticalAlignment="Top"Width="75"Click="button1_Click"/>
</Grid>
</usercontrol>
Step 8: The complete code of MainPage.xaml.cs looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightRIASelectApp.Web;
using System.ServiceModel.DomainServices.Client;
namespace SilverlightRIASelectApp
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
EmployeeDomain objDomain = new EmployeeDomain();
private void button1_Click(object sender, RoutedEventArgs e)
{
LoadOperation<Employee> load = objDomain.Load(objDomain.GetEmployeeQuery());
dataGrid1.ItemsSource = load.Entities;
}
}
}
Step 9: Output of the application looks like this:
Step 10: Data showing output of application looks like this:
I hope this article is useful for you.