How to create a synchronized Online/Offline data application with EntityFramework, JavaScript and JayData

Author: Peter Aron Zentai September 27th, 2012

This post is a part of a series, find more here:

There are quite a few scenarios when an offline and an online version of the database (or a subset of it) is required to provide some application features. Such scenario can be an application that is not all the time connected to the net but needs to collect data (offline) for later submission. Another example can be an offline cache on the client for faster data access or to preserve network bandwidth or to provide all time access to some data regardless of the network state.

In this post we will focus on the first scenario, “collect locally, push to server for shared access”.  Upcoming posts will detail Online to Offline, and Local Data Cache scenarios.

The architecture

In our example we will create an offline capable HTML5 Application that will collect some user input (Task items) and store it in its persistent local database. Every once in a while we push all the new items to the server using REST and the HTTP protocol to store data online. Collecting user input and submitting new items to the server will be two decoupled workflows. This allows for seamless user experience even if network status goes offline or changes frequently.

Data model

The “TodoItems” table

TodoItems will be our only entity type for now. It will have a Task and a Completed field to store use input, and an InSync boolean field to keep track of new items.

The key field

Synchronization can be implemented with any key type, but there is one type that is especially optimal for it: the UUID/ GUID / $data.Guid, it has many names. The GUID type lets us share auto generated primary key values between the client and server.

Client and Server data model

We are about to share the very same model definition between the server and client store when creating the application. This simplifies a lot in the application code, as TodoItems will be able to come and go freely between the layers, so we don’t have to transform offline TodoItem to online TodoItem. And also their item ID’s will not collide either.

Server tier

We need to build an OData compatible REST service. This can be done with Entity Framework and WCF Data services, or ASPNET WebAPI. Or we can use a cloud based OData service as well.

Client tier

On the client we will use JayData as the data abstraction layer to provide a common developer experience over handling data regardless it’s being online or offline or in between. Under the storage agnostic EntityContext surface four JayData providers will be engaged, depending the client environment capabilities IndexedDB, webSQL or HTML5 localStore for storing offline data and the OData provider to communicate with the server.

Let’s build the application

Create the online data store

We will create an OData compatible REST service endpoint to accept Todo items from the clients. It has a number of possible ways, pick the one that fits your needs the best. Currently only the EF + WCF has a detailed walkthrough here.

  • Entity Framework 5.0 and WCF Data Services to store data in an SQL Server and if you don’t really want to write lot’s of code.
  • ASP.NET WebAPI if you need a code based backend solution.
  • JayStorm Open if you need an easy to use cloud solution.

No matter which option we take, we will conclude in a service with the following metadata:


Create The Client Application

Our client app will be a simple Single Page Application. If you created your online service with Visual Studio, simply open that project and start extending it with the following. If your OData endpoint is a cloud service then just start a new Empty Web project.

Open package manager console and add two packages: JayData and jQuery.

Along with a bunch of script files with JayData package you get the JaySvcUtil.exe that creates JavaScript client classes from a WCF Data Service / OData endpoint metadata. We will use this utility to generate the TodoItem and the TodoContainer classes. We will eventually highjack types generated from the OData endpoint and will use them for the webSql storage as well.

Use JaySvcUtil to generate client data classes

Open a command prompt and navigate to you website project folder and invoke JaySvcUtil.exe with the –metadataUri param pointing to http://youlocalsite/OnlineDB.svc/$metadata


A new file TodoDB.js is generated and placed in your web site folder. You may have to press “Show all items” in the solution explorer to see. An occasional refresh may also help. Right click on the file to include in the project. Peeking into it you should see this:

Contents of the TodoDB.js file

We could use these classes without any further ado, but to make the following parts easier to read, let’s do some modifications. Rename TodoDBModel.Item to just Item, OfflineDataCollector.TodoDBEntities to TodoDB, make sure to update the elementType as well, and remove the context instantiation part from the end. You should have to come to this:

Contents of the modified TodoDB.js file

Add a new HTML page

Create and index.html page and reference jQuery, datajs, JayData and the newly forged TodoDB.js  plus add the following additional script block to instantiate a local and a remote context from these classes:

Navigate to your index.html with a browser that supports WebSql: Chrome or Safari. We will get into supporting other browsers later in this article.

If all is well you should see the new local database in your browsers console by press F12 or going into the Develop menu in Safari and selecting Resources.


Generate and display local data

With our online and offline services now set up, we just need to create some offline data, storing it many TodoItem in our offlinedb.

Create the UI to collect user input and list local TodoItems

Create the code to add new TodoItems and list existing ones

Launch your single page application, and add some tasks:



Submit new data to server

First some rudimentary UI: display two columns of tasks. First column shows local database content, while the second will display online items. We revisit the view we created before:

Let’s create code for pushing new (InSync === false) items to the server, and setting InSync to true for items that has been successfully saved online. The essence of pushing items to the server is this routine:

This is where we arrive. Try adding new items and pressing Synchronize a number of times to see how it works.



This is how the index.html page should look like inside:

Going offline capable

Now that you have a fully working HTML5 application that is able to store data locally, the next step is to turn it into an app that is able to run offline, without network connection present. To do that you have a number of options. You can build a Cordova app from these files to deploy as an App Store application. Or you can use a cache manifest file so that you HTML5 app is persisted locally on the device and can start regardless of being online or offline.