12/1/09

getting started with Silverlight consuming ADO.NET Data Service


Start new Silverlight project (I am using Silverlight 3 here)


Give it a name, click OK. 
Then “New Silverlight Application” host comes up. 

Click OK. This will create SilverlightApplication and SilverlightApplication.Web projects in your solution.

Add Add ADO.NET Entity Data Model to your SilverlightApplication.Web project



Entry Data Model Wizard will show, select Generate from Database, choose or create your database connection, your database objects. 



Note your entities name, you'll use it later in when adding it to the WebDataService as your data service type (I named mine MyDbEntities)
 

Add WebDataService to your SilverlightApplication.Web



In WebDataService.svc.cs add your Entity

public class WebDataService : DataService< /* TODO: put your data source class name here */ > 
public class  WebDataService :  DataService<MYDBEntities >
also in the same file there config rights, for now to get things going I’d put “*” in both…

config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
Build the SilverlightApplication.Web (so that you can reference the service in your SilverlightApplication project)

Add a Service Reference to SilverlightApplication project (click Discover if need to)




Finally, done with the setup. Lets add little code:

In SilverlightApplication project, the MainPage.xaml the page already has a Grid x:Name=”LayoutRoot”
Add a grid to it:

<data:DataGrid x:Name="dataGrid"/>

In MainPAge.xaml.cs:
Create a private field "entities", instanciate it, query, then set the dataGrid.ItemSource to what you got back…

Notes about the code below:

1. entities = new MYDBEntities(new Uri("http://localhost:3431/WebDataService.svc", UriKind.RelativeOrAbsolute));
if not sure about the Uri, then right-click on WebDataService.svc > View in Browser, to copy the link …also the port number is just a random port being assigned. If you add your service to IIS, then you can use relative url.

2. if IntelliSence doesn’t come up, or you're not sure what the name of your entities are (mine is MYDBEntities), open Model.Designer.cs and look for a class that derives from ObjectContext.

3. var query = entities.users; //users is just a table in my db… user yours
Here's the code...


public partial class MainPage
    {
        private MYDBEntities entities;
        public MainPage()
        {   

           InitializeComponent();
           entities = new MYDBEntities(

                        new Uri("http://localhost:3431/WebDataService.svc", 
                        UriKind.RelativeOrAbsolute));
           var query = entities.users;
           try
           {
              query.BeginExecute(c => 

              { 
                dataGrid.ItemsSource = query.EndExecute(c).ToList();
              }, query);
           }
           catch (Exception exception)

           {
              MessageBox.Show(exception.ToString());
           }
        }
    }



that's it, run it, and you should see some data in the grid.


No comments:

Post a Comment