Subscribe RSS Join our Facebook Group Follow us on Twitter!
in Search

All about GIS

July 2011 - Posts

  • Creating an ADO.NET Data Service

    In my previous blog posts, I have wrote about how to consume an Open Data Protocol (OData) feed. In those posts, I used the NerdDinner OData end point. Right now, maybe a few of you are wondering, how can I make that OData service in .NET? Well, it’s actually not as hard as you thought it would be and I’m going to explain it how. Oh I almost forgot, in this example, I will use the Northwind sampe database which can be downloaded in here.

    1. Create a new WCF Service application

    New WCF Service Application

    If you look at the solution explorer, you will see that we have IService1.cs and Service1.svc. Just delete these two files. We don’t need them anyway.

    Service

    2. Define the data model

    To define the data model, I’m going to use Entity Framework. Maybe you are going to ask me, why am I using the Entity Framework? Well, it’s simply because Entity Framework make this task a whole lot easier so we can save time. To create an Entity Framework, you need to add a new ADO.NET Entity Data Model. I will name it Northwind.edmx because I’m using the Northwind database.

    ADO.NET Entity Data Model

    In the Entity Data Model Wizard, select Generate from Database.

    Entity Data Model Wizard

    Then choose your connection and create the connection string.

    Data Connection

    Pick the tables that you want to publish and click Finish.

    Database Objects

    3. Create the data service

    OK, we already have the data model so it’s time to create the data service. We need to add a new WCF Data Service to our project.

    WCF Data Service

    After the WCF Data Service have been created, you need to change the /* TODO: put your data source class name here */ with the name of the entity container of the data model, which in my case is NorthwindEntities.

    4. Enabling access to data service resources

    Put the code below to the InitializeService function in Northwind.svc.cs file.

    config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);

    And that’s it. You can now run the application. But before you do, maybe you need to turn off the feed reading view in Internet Explorer which is located in Internet Options->Content and choose Settings in the Feed and Web Slices then uncheck the Turn on feed reading view. After you run the application, it will appear something like this:

    Result

    Just test that out with the OData query in here. Enjoy…

    467 Views, 0 Comment(s), Published on: 07-04-2011 20:15 by veri to All about GIS
    | More