Effective EF with Oracle – RIA & Silverlight 3

by Jonathan Bruce on August 31, 2009

in .NET

Over the past few months we received numerous how-to requests on how best to wire up a Silverlight 3 application with ADO.NET EF, ADO.NET Data Services and Oracle for use in an RIA application. To meet that need, we’ve put together how-to that we are now publishing on our .NET Connections blog. Full credit goes to our team over at PSI, specifically Avadhoot Kulkarni for doing the majority of leg work here.

First make sure you have all the prerequisites, such as Microsoft Visual Studio 2008 SP1, Microsoft .NET framework 3.5 SP1, Microsoft Silverlight 3, Microsoft Silver-light plug-in for Visual Studio 2008 SP1 and of course Progress DataDirect Connect for ADO.NET 3.3 to connect to Oracle.

Let’s start with creating database schema on our Oracle database. I will create a single table called Categories.

CREATE TABLE "Categories" (
"CategoryID" NUMBER (10,0) NOT NULL,
"CategoryName" VARCHAR2 (15) NOT NULL,
"Description" LONG NULL,
"Picture" BLOB NULL,
CONSTRAINT "PK_Categories" PRIMARY KEY("CategoryID")
)

And we’ll populating the table much as you’d expect.

INSERT INTO "Categories" ("CategoryID", "CategoryName", "Description", "Picture") VALUES(1,'Beverages','Soft drinks, coffees, teas, beers, and ales')
INSERT INTO "Categories" ("CategoryID", "CategoryName", "Description", "Picture") VALUES(2,'Condiments','Sweet and savory sauces, relishes, spreads, and seasonings')
INSERT INTO "Categories" ("CategoryID", "CategoryName", "Description", "Picture") VALUES(3,'Confections','Desserts, candies, and sweet breads')
INSERT INTO "Categories"("CategoryID","CategoryName","Description","Picture") VALUES(4,'DairyProducts','Cheeses')
INSERT INTO "Categories"("CategoryID","CategoryName","Description","Picture") VALUES(5,'Grains/Cereals','Breads, crackers, pasta, and cereal')
INSERT INTO "Categories"("CategoryID","CategoryName","Description","Picture") VALUES(6,'Meat/Poultry','Prepared meats')
INSERT INTO "Categories"("CategoryID","CategoryName","Description","Picture") VALUES(7,'Produce','Dried_fruit and bean curd')

Next up, we’ll create a new ASP.NET Application project in Visual Studio.

1.      Add a new Entity Model for our Categories using the Entity Data Model wizard in Visual Studio 2008. Check MSDN for more details here.

2.      Next, add new ADO.NET data Services component to the ASP.NET project name it as ‘CategoriesDataService1′. Modify CategoriesDataService1.svc file with the required Initialization rules as follows.

public class CategoriesDataService1 : DataService<Entities>
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(IDataServiceConfiguration config)
        {
// TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
               config.SetEntitySetAccessRule("*", EntitySetRights.All);
               config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
        }
    }

Configure your ASP.NET application to run on Specific port 50000 (for example) in the project properties\Web

 

Run the ASP.NET Application and browse our CategoriesDataService1.svc through browser. Now we can browse data through URLs like all Categories, as you’d expect.

Or some specific category identified by its Entity key property. [In this case CategoryID]

Now, let’s spin up a Silverlight application in a new Visual Studio instance, while our new Data Service application runs. We’ll add the Data Service reference to Silverlight app using “Add Service Reference” wizard.

You’ll next need to modify the Mainpage.xaml which defines our Silverlight UI. Notice it has a “Get Categories” button and a Data Grid with title “Categories”.

<Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal">
            <Button Content="Get Categories" Click="Button_Click" Margin="5"  ></Button>
        </StackPanel>
        <data:DataGrid x:Name="DataGrid1"  AutoGenerateColumns="True" Grid.Row="1">
            <data:DataGrid.Columns >
                <data:DataGridTemplateColumn Header="Categroies">
                    <data:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding CategoryName, Mode=TwoWay}"></TextBlock>
                        </DataTemplate>
                    </data:DataGridTemplateColumn.CellTemplate>
                    <data:DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <TextBox Tag="{Binding CategoryID}" Text="{Binding CategoryName, Mode=TwoWay}" ></TextBox>
                        </DataTemplate>
                    </data:DataGridTemplateColumn.CellEditingTemplate>
                </data:DataGridTemplateColumn>
            </data:DataGrid.Columns>
        </data:DataGrid>
    </Grid>

To ensure compile nicely, we need to add reference to the System.Windows.controls.Data to our Silverlight project and a namespace declaration in User Control block.

xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"

Next up, open MainPage.xaml.cs to implement the “Button_Click” event handler. Add following lines to the .cs file.

private void Button_Click(object sender, RoutedEventArgs e)
        {
DataServiceQuery<ServiceReference1.Categories> query = (DataServiceQuery<ServiceReference1.Categories>)
                from c in entities.Categories
                where c.CategoryID > 5
                select c;
            DataGrid1.ItemsSource = null;
            query.BeginExecute(new AsyncCallback(c =>
            {
                IEnumerable<ServiceReference1.Categories> results = query.EndExecute(c);
                loadedProducts = results.ToList();
                DataGrid1.ItemsSource = loadedProducts;
           }), query);
        }

Now you’re ready to run and enjoy your ADO.NET EF aware Silverlight application!.

Bookmark and Share

{ 3 comments… read them below or add one }

1 jaskirat December 17, 2009 at 12:02 am

I tried the above example, but I’m getting an error public class

“The type or namespace name ‘Entities’ could not be found(are you missing a using directive or an assembly reference?”

in the below statement:
CategoriesDataService1 : DataService

suggest something.

2 Avadhoot Kulakrni December 23, 2009 at 4:36 am

jaskirat,

I think you have a mismatch between the ObjectContext derived class in your edmx Object classes in the ASP.NET application.

should look something like
public partial class Entities : global::System.Data.Objects.ObjectContext

and the one your are trying to use .
public class CategoriesDataService1 : DataService

Please check the correct name in the edmx and use the same.

3 Anon December 28, 2009 at 12:05 pm

This article actually does not use RIA at all but rather uses ADO.NET Data Services so the article should probably be re-written to clarify this.

Leave a Comment

Previous post:

Next post: