The Entity Data Model (EDM) is an Entity-Relationship data model. The key concepts introduced by the EDM are:
connectionString="metadata=res://*/EmployeeModel.csdl|res://*/EmployeeModel.ssdl|res://*/EmployeeModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=B314LTRV\SQLEXPRESS;Initial Catalog=EmployeeDB;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" /> </connectionStrings></configuration>ListingNow we will add a Service that will fetch data from the Entity Model and display it in the Silverlight application. Create a Silverlight enabled WCF Service and give it a proper name.Figure 13 After you have added the Service you can see the updated web.config file in the Web Project. The Service model settings are added here in the web.config file.<?xml version="1.0" encoding="utf-8"?><configuration> <system.web> <compilation debug="true"> <assemblies> <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> </assemblies> </compilation> </system.web> <connectionStrings> <add name="EmployeeDBEntities"connectionString="metadata=res://*/EmployeeModel.csdl|res://*/EmployeeModel.ssdl|res://*/EmployeeModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=B314LTRV\SQLEXPRESS;Initial Catalog=EmployeeDB;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" /> </connectionStrings> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name=""> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> <bindings> <customBinding> <binding name="DataAccessSampleLINQ.Web.EmployeeService1.customBinding0"> <binaryMessageEncoding /> <httpTransport /> </binding> </customBinding> </bindings> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> <services> <service name="DataAccessSampleLINQ.Web.EmployeeService1"> <endpoint address="" binding="customBinding" bindingConfiguration="DataAccessSampleLINQ.Web.EmployeeService1.customBinding0" contract="DataAccessSampleLINQ.Web.EmployeeService1" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> </system.serviceModel></configuration>ListingNow we will do the same thing that we did earlier and that is to display all the Employees in the client. So let's add an Operation Contract and name it GetAllEmployees(), which will return a List of type Employee. Again this Employee is the same as we are having the Class in Entity Model.[ServiceContract(Namespace = "")] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class EmployeeService1 { [OperationContract] List<Employee> GetAllEmployees() { EmployeeDBEntities entity = new EmployeeDBEntities(); var result = from emp in entity.Employees select emp; return result.ToList(); } }ListingAs you see in the preceding listing, in the basic change from LINQ to SQL we are using EmployeeDBEntities instead of DataContext. The remaining code is the same. Now we will add a service reference to the Silverlight client project.Figure 14We need to discover to find out what Services are available; after you see one you can actually see the methods exposed in the service too, the following figure shows the GetEmployees method.Figure 15As you see in the image above, give a name to the namespace or we can use the default name. After you press the OK button the service is referenced and a ServiceReference.ClientConfig file is created.Figure 16After you have added the service you will see the following error message saying "Failed to generate code for the service reference 'ServiceReference1'".Figure 17To generate the code automatically we need to configure the Service Reference.Figure 18On selecting Configure Service Reference you will see a dialog for Service Reference Settings. Uncheck "Reuse types in referenced assemblies"Figure 19Now update the Service Reference and you will see no errors and the ServiceReference.ClientConfig file is successfully auto generated.<configuration> <system.serviceModel> <bindings> <customBinding> <binding name="CustomBinding_EmployeeService1"> <binaryMessageEncoding /> <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" /> </binding> </customBinding> </bindings> <client> <endpoint address="http://localhost:62782/EmployeeService1.svc" binding="customBinding" bindingConfiguration="CustomBinding_EmployeeService1" contract="ServiceReference1.EmployeeService1" name="CustomBinding_EmployeeService1" /> </client> </system.serviceModel></configuration>ListingAs we have added a DataGrid to our MainPage.xaml for displaying data from the service, and we have changed to show the Columns properly according to our preference, now we will write code to create an instance of ServiceReference and bind data to the DataGrid.public MainPage(){InitializeComponent();EmployeeService1Client serviceClient = new EmployeeService1Client();serviceClient.GetAllEmployeesCompleted += new EventHandler<GetAllEmployeesCompletedEventArgs>(service_GetAllEmployeesCompleted);serviceClient.GetAllEmployeesAsync(); }
void service_GetAllEmployeesCompleted(object sender, GetAllEmployeesCompletedEventArgs e){dgEmployee.ItemsSource = e.Result;}ListingNow run the application and you will see the same result as we saw in the case of LINQ to SQL.Figure 20
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: