JayData.org

How to create a synchronized Online/Offline data application with EntityFramework– the Online data service

Author: Peter Aron Zentai October 28th, 2012


This document is a part of a series detailing the creation of an offline/online data application client. In this post we show how to create an online data endpoint with Entity Framework and Visual Studio 2012 that can be used by AJAX/HTML5 clients . What we are about to build is a simple SQL Server backed data table, animated by an Entity Framework data context, exposed by WCF Data Services as an OData endpoint.

Define data store

Open Visual Studio 2012 and create a new Empty Web Application, let’s call it OfflineDataCollector web application. Right on the project, and “Add ASP.NET folder”, select the App_Data folder. When created right clicking on the App_Data folder, and add new item “SQL Server Compact 4.0 Local Database”, and name it OnlineDB.sdf. You can use any kind of SQL Server database not just the compact edition.

Now right click on the OfflineDataCollector project and add a new ADO.NET Entity Data Model, and name it OnlineDB.edmx. Select “Empty Model” to open up the EDMX designer where we will define our Items table. Add four fields: Id, Data, Syncronized, CreationDate with types: Guid, String, Boolean and DateTime respectively. When defining the Id field, make sure that its store generated pattern is “None”, so that new entities can get their ids from the client.

CropperCapture82_thumb1

When ready right click on the data model, and select generate database from model, where you select OnlineDB as suggested.

CropperCapture83_thumb2

Press next all the way, and the OnlineDB.edmx.sqlce file is generated. There are a number of ways to have this sql applied on the OnlineDB.sdf database. One of them is using SqlCeToolbox from MS, available here in case you wouldn’t already have it. Open it from the View menu | Other Windows | Sql Server Compact Toolbox. Right click on OnlineDBContainer and select “Open SQL Editor”. Paste the contents of the OnlineDB.edmx.sqlce file into the editor and press Execute.

CropperCapture86_thumb2

Some parser error might display, but fear not, your db is still created:

CropperCapture87_thumb1

Expose as WCF Data Service/OData

Now that we have a working database, let’s expose it with WCF Data Services. Right click on the OfflineDataCollector project, to add a new WCF Data Service to your solution. (This you find the easiest way by selecting Visual C# (or Basic) in the template tree, then type “WCF Data” into the search box. Name your service OnlineDB.svc

CropperCapture88_thumb1

Open the newly added svc file, and fill in the blanks and set access rules:

You are ready with the service side, to make sure everything’s ok, select OnlineDB.svc in the solution explorer and press F5 to see it running. If all is well, you should see this XML:

CropperCapture89_thumb1

To continue building the client code, read this.

This document is a part of a series detailing the creation of an offline/online data application client. In this post we show how to create an online data endpoint with Entity Framework and Visual Studio 2012 that can be used by AJAX/HTML5 clients . What we are about to build is a simple SQL Server backed data table, animated by an Entity Framework data context, exposed by WCF Data Services as an OData endpoint.

Define data store

Open Visual Studio 2012 and create a new Empty Web Application, let’s call it OfflineDataCollector web application. Right on the project, and “Add ASP.NET folder”, select the App_Data folder. When created right clicking on the App_Data folder, and add new item “SQL Server Compact 4.0 Local Database”, and name it OnlineDB.sdf. You can use any kind of SQL Server database not just the compact edition.

Now right click on the OfflineDataCollector project and add a new ADO.NET Entity Data Model, and name it OnlineDB.edmx. Select “Empty Model” to open up the EDMX designer where we will define our Items table. Add four fields: Id, Data, Syncronized, CreationDate with types: Guid, String, Boolean and DateTime respectively. When defining the Id field, make sure that its store generated pattern is “None”, so that new entities can get their ids from the client.

CropperCapture82_thumb1

When ready right click on the data model, and select generate database from model, where you select OnlineDB as suggested.

CropperCapture83_thumb2

Press next all the way, and the OnlineDB.edmx.sqlce file is generated. There are a number of ways to have this sql applied on the OnlineDB.sdf database. One of them is using SqlCeToolbox from MS, available here in case you wouldn’t already have it. Open it from the View menu | Other Windows | Sql Server Compact Toolbox. Right click on OnlineDBContainer and select “Open SQL Editor”. Paste the contents of the OnlineDB.edmx.sqlce file into the editor and press Execute.

CropperCapture86_thumb2

Some parser error might display, but fear not, your db is still created:

CropperCapture87_thumb1

Expose as WCF Data Service/OData

Now that we have a working database, let’s expose it with WCF Data Services. Right click on the OfflineDataCollector project, to add a new WCF Data Service to your solution. (This you find the easiest way by selecting Visual C# (or Basic) in the template tree, then type “WCF Data” into the search box. Name your service OnlineDB.svc

CropperCapture88_thumb1

Open the newly added svc file, and fill in the blanks and set access rules:

You are ready with the service side, to make sure everything’s ok, select OnlineDB.svc in the solution explorer and press F5 to see it running. If all is well, you should see this XML:

CropperCapture89_thumb1

To continue building the client code, read this.