Working With Entity Framework Code First And JSON In ASP.NET Web Pages

If you want to build AJAX powered rich client interfaces with ASP.NET Web Pages, you will work with JSON - a lot. You might also be attracted to the Entity Framework for your data access technology. There are some special considerations to take account of when serialising Entity Framework objects to JSON for use in Web Pages applications. This article explores some of the issues you will encounter through the example of a simple To Do list manager.

The sample application is very, very simple: it displays items from a To Do list and provides the means to add new items. The object model is also very simple. Each item in the To Do list is realised in code as a Task object. Each task results in an activity which is defined as a TaskType object. This exercise follows the Code First approach to using the Entity Framework. If you are not familiar with Code First and how to add the Entity Framework to your site, you might want to read my previous article covering the topic. Assuming you are confortable with the basics, here is the code for the entities for this application. First, the Task class:

using System;
using System.ComponentModel.DataAnnotations;

/// <summary>
/// Represents an item in a TO DO list
/// </summary>
public class Task
    public int TaskId { get; set; }
    [Required, MaxLength(100)]
    public string TaskTitle { get; set; }
    [Required, MaxLength()]
    public string Details { get; set; }
    public DateTime CompleteBy { get; set; }
    public bool IsDone { get; set; }
    public int TaskTypeId { get; set; }
    public virtual TaskType TaskType { get; set; }

Now the TaskType class:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

/// <summary>
/// Represents the expected output of a task 
/// </summary>
public class TaskType
    public int TaskTypeId { get; set; }
    [Required, MaxLength(50)]
    public string Activity { get; set; }
    public virtual ICollection<Task> Tasks { get; set; }

Both of these are placed a separate files (Task.cs and TaskType.cs) in the App_Code folder so that they are compiled when the application first runs. In addition, a class that derives from DbContext is needed:

using System.Data.Entity;

/// <summary>
/// Enables data access for entities
/// </summary>
public class TaskContext : DbContext
    public DbSet<Task> Tasks { get; set; }
    public DbSet<TaskType> TaskTypes { get; set; }

The DbSets will become tables in the database that gets generated by EF and an instance of TaskContext will provide the means to execute SQL against the database using C# code rather than SQL. The C# code will be based on the Repository pattern, with data access methods grouped by entity. The TaskRepository code is as follows:

using System;
using System.Collections.Generic;
using System.Linq;

/// <summary>
/// Centralises data operations for Task objects
/// </summary>
public class TaskRepository
    TaskContext db = new TaskContext();

    public Task Save(Task task) {
        task.TaskType = db.TaskTypes.Find(task.TaskTypeId);
        if (task.TaskId == 0) {
        else {
            var t = db.Tasks.Find(task.TaskId);
                t.IsDone = task.IsDone;
            } else {
                t.TaskTitle = task.TaskTitle;
                t.Details = task.Details;
                t.CompleteBy = task.CompleteBy;
                t.TaskType = task.TaskType;
        return db.Tasks.Find(task.TaskId);

    public List<Task> GetAllTasks(){
        return db.Tasks.ToList();

There are two methods. The first is a Save method, which covers both inserts and updates. It accepts a Task obejct as a parameter. If that Task object has no TaskId, it has yet to be created, so the Task is added as a new entry to the Tasks DbSet. If there is a TaskId, the object exists and needs to be modified. If IsDone is true, that is considered to be the only change required. Otherwise the Task properties are amended. Finally, the changes are committed to the database and the new or modified task object is returned. The second method returns all tasks returned as a generic List.

The TaskTypeRepository code consists of one method that returns all TaskType objects as a List:

using System;
using System.Collections.Generic;
using System.Linq;

/// <summary>
/// Centralises data operations for TaskType objects
/// </summary>
public class TaskTypeRepository
    TaskContext db = new TaskContext();
    public List<TaskType> GetAllTaskTypes(){
        return db.TaskTypes.ToList();

These methods are going to be used by a set of services, which will be responsible for translating the entities generated by the repositories to JSON, and for translating JSON that is generated by the calling page into entity objects that the repository methods can work with. Each service will occupy its own .cshtml file. So I create a folder called JsonService, and add three files to it: GetAllTaskTypes.cshtml, GetAllTasks.cshtml and SaveTask.cshtml. GetAllTaskTypes.cshtml contains the following code:

    var repository = new TaskTypeRepository();
    var taskTypeList = repository.GetAllTaskTypes().Select(t => new {
        TaskTypeId = t.TaskTypeId,
        Activity = t.Activity
    Json.Write(taskTypeList, Response.Output);

The service uses the Json helper to generate JSON, but it doesn't serialise the List<TaskType> that the repository returns. The List<TaskType> is converted into an anonymous type and that is what is serialsed to JSON. You can see this where a LINQ Select extension method has been added to the repository method call, and a lambda expression takes each element in the sequence and converts it to a different type. However, no type is specified after the new keyword, so the compiler generates a type on the fly. Anonymous types are usually used as convenience containers for read-only property values.

Why convert the TaskType collection to an anonymous type as allt? Well, each TaskType object has a reference to a Task collection, and each Task object has a reference to a TaskType object. The serialiser within the Json helper cannot cope with the resulting circular reference, and if an attempt is made to serialise TaskType objects, or Task object, an exception is thrown:

Exception Details: System.InvalidOperationException: A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.<T> (where T is the type being serialised.)

There are a couple of ways to prevent this, but the simplest is to "flatten" the data and thereby remove any references it has prior to serialising it. This is achieved by projecting it into a new form, and an anonymous type is perfect for that. The same approach using an anonymous type is taken in the GetAllTtasks.cshtml code:

    var repository = new TaskRepository();
    var taskList = repository.GetAllTasks().Select(t => new {
        TaskId = t.TaskId,
        TaskTitle = t.TaskTitle,
        Details = t.Details,
        CompleteBy = t.CompleteBy.ToString("yyyy/MM/dd"),
        IsDone = t.IsDone,
        Activity = t.TaskType.Activity
    Json.Write(taskList, Response.Output);

The Task object's CompleteBy property is a DateTime data type. Left alone, it will serialise in a particular format, ege: \/Date(1343430000000)\/ where the number represents the elapsed milliseconds since Jan 1 1970. That would require some manipulation in client code to get it to appear as a recognisable date. If I was exposing the JSON to third parties I would probably just allow the JSON helper to do its thing and let others worry about how to display the date. But as this is for internal use only, I choose to convert the value to a string on the server, and then serialise that. It saves having to construct some additional JavaScript for formatting the value in the browser.

The final "service" for this application is the one that is responsible for taking a new or modified Task and saving it to the database, which is the responsibility of SaveTask.cshml:

    var reader = new StreamReader(Request.InputStream);
    var json = reader.ReadToEnd();
    var task = Json.Decode<Task>(json);
    var repository = new TaskRepository();
    var t = repository.Save(task);
    var savedTask = new {
        TaskId = t.TaskId,
        TaskTitle = t.TaskTitle,
        Details = t.Details,
        CompleteBy = t.CompleteBy.ToShortDateString(),
        IsDone = t.IsDone,
        Activity = t.TaskType.Activity
    Json.Write(savedTask, Response.Output);

There are a number of ways in which you can transfer an object from client-side JavaScript to the server via AJAX. The first is to serialise an HTML form, using POST. You can obtain the values from Request[form-field-name] on the server. You could also send a bunch of name/value pairs in the query string using GET, also accessible via Request[form-field-name]. Or you can construct a JSON object and send it in the body of a POST request. If you do that, the value is available in the InputStream property of the Request object. That is0 how the SaveTask service expects a Task object to arrive - in the body of the POST request. The value is stored in a variable called json once it has been extracted from the InputStream, and then is converted into a C# Task obejct using the strongly typed Json.Decode<T> method, where T represents the type to decode the JSON into. That Task object is passed ot the repository's Save method which will either add a new task or modify and existing one, and then return the new or modified task back to the service. That is prepared for serialisation and sent back to the client.

So to recap - we have our objects defined - Task and TaskType. We have a way of mapping those objects to database tables via our TaskContext object which derives from DbContext. We have some simple data access methods in repository classes, and we have a way of making use of those data access methods from client-side Javascript. We do not actually have a database, neither do we have any client-side code for displaying or adding new task objects. We'll take care of the lack of database first.

This sample won't provide a means to create TaskTypes from the user interface, so they will be created from code. One of the features of Code First is that the database is not generated until code attemtps to make use of it, so we can kill both birds with one stone by adding some code in and AppStart file:

@using EF = System.Data.Entity;
    EF.Database.SetInitializer(new EF.DropCreateDatabaseIfModelChanges<TaskContext>());
    var db = new TaskContext();
    db.TaskTypes.Add(new TaskType {Activity = "Email"});
    db.TaskTypes.Add(new TaskType {Activity = "Meeting"});
    db.TaskTypes.Add(new TaskType {Activity = "Document"});
    db.TaskTypes.Add(new TaskType {Activity = "Phone Call"});
    db.TaskTypes.Add(new TaskType { Activity = "Letter" });

AppStart executes when the site first starts. Consequently, this code will result in the database being created and a series of task types being added to the TaskType table. If the site is restarted, the TaskTypes will be added again, so you should remember to remove the code that does this once the database has been created and populated.

That just leaves the user interface, which can be accommodated in a single page. Here it is in its entirety:

<!DOCTYPE html>
<html lang="en">
        <meta charset="utf-8" />
        <title>TODO Manager</title>
        <link href="~/Content/site.css" rel="stylesheet" type="text/css" />
        <link href="~/Content/jquery-ui-1.8.21.custom.css" rel="stylesheet" type="text/css" />
        <script src="~/Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
        <script src="~/Scripts/jquery-ui-1.8.21.custom.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            //Controls appearance of new tasks
            function slide(panel, content){
                $('#' + panel).slideDown(600, function(){
                    $('#' + content).animate(
                            opacity: '1'
                        },  600

            $(function () {
                // Prevent browser caching
                $.ajaxSetup({ cache: false });
                var taskList = $('#tasks');
                var select = $('#TaskTypeId');

                //Get all tasks and display them
                $.getJSON('/JsonService/GetAllTasks', function(tasks) {
                    $.each(tasks, function(index, task) {
                        var panel = 'panel' + task.TaskId;
                        var content = 'content' + task.TaskId;
                        var html = '<div id=\'' + panel + '\' class=\'panel\'><div id=\'' + content + '\' class=\'content\'>';
                        html += '<p><strong>' + task.TaskTitle + '</strong></p>';
                        html += task.Details + '<br />';
                        html += 'Complete By: ' + task.CompleteBy + '<br />';
                        html += task.Activity + '</div></div>';
                        slide(panel, content);

                // Populate the dropdwnlist with Task Types
                $.getJSON('/JsonService/GetAllTaskTypes', function(taskTypes) {
                    select.append($('<option/>').attr('value', '').text('--Choose Task Type--'))
                    $.each(taskTypes, function(index, taskType) {
                        select.append($('<option/>').attr('value', taskType.TaskTypeId).text(taskType.Activity));

                // Set up the date picker
                $('#CompleteBy').datepicker({ dateFormat: 'yy/mm/dd' })

                // Open the modal form on the button click 
                $('#add-task').click(function() {

                // Convert the div to a modal dialog form
                    autoOpen: false,
                    modal: true,
                    height: 315,
                    width: 500,
                    buttons: {
                        'Add Task': function () {
                            // Create a new task object
                            var task = {
                                TaskTitle: $('#TaskTitle').val(),
                                Details: $('#Details').val(),
                                CompleteBy: $('#CompleteBy').val(),
                                TaskTypeId: $('#TaskTypeId').val()
                            // Add the new task and display it
                            $.ajax("JsonService/SaveTask", {
                                data: JSON.stringify(task),
                                type: "post",
                                contentType: "application/json",
                                success: function(data) {
                                    var savedTask = JSON.parse(data);
                                    var panel = 'panel' + savedTask.TaskId;
                                    var content = 'content' + savedTask.TaskId;
                                    var html = '<div id=\'' + panel + '\' class=\'panel\'><div id=\'';
                                    html += content + '\' class=\'content\'>';
                                    html += '<p><strong>' + savedTask.TaskTitle + '</strong></p>';
                                    html += savedTask.Details + '<br />';
                                    html += 'Complete By: ' + savedTask.CompleteBy + '<br />';
                                    html += savedTask.Activity + '</div></div>';
                                    slide(panel, content);
                        Cancel: function() {
        <button id="add-task">Add Task</button>
        <div id="tasks"></div>
        <div id="new-task" title="New Task">
            <div class="editor-row">
                <span class="editor-label">Title:</span>
                <span class="editor-field">@Html.TextBox("TaskTitle", null, new {size = 40})</span>
            <div class="editor-row">
                <span class="editor-label">Details:</span>
                <span class="editor-field">@Html.TextArea("Details", new {rows = 6, cols = 50})</span>
            <div class="editor-row">
                <span class="editor-label">Complete By:</span>
                <span class="editor-field">@Html.TextBox("CompleteBy")</span>
            <div class="editor-row">
                <span class="editor-label">Task Type:</span>
                <span class="editor-field">@Html.DropDownList("TaskTypeId", Enumerable.Empty<SelectListItem>())</span>

Starting at the bottom, you can see a button, an empty div and a series of form fields, but no form element. The empty div called tasks is used to display the list of tasks, and the button is used to invoke the jQuery dialog whcih will display the form fields. The task type selector is generated using the Html.DropDownList helper. It is prepopulated with an empty collection. When the page loads, a jQuery getJSON method is called that obtains the TaskType data from GetAllTaskTypes.cshtml and populates the dropdown list with them. Another getJSON call is repsonsible for obtaining any existing Tasks and displying them in the empty div. When the dialog fields are completed and the Save button is clicked, a JavaScript Task object is created from the inputs and then converted to JSON using JSON.stringify (a browser method, not specific to jQuery) and posted to the SaveTask.cshtml service. The return value is added to any that already appear, and a bit of jQuery animation is used to jazz the appearance up a bit. You can clone the GitHub repo that features the code in this article and experiment with it as you like. It is set up ready to create the database and populate TaskTypes on the first run.


The key concepts covered in this article are: how to design Entity Framework data access based on the Repository pattern; the need to flatten Entity Framework objects before serialising to JSON; how to use JSON.stringify() to convert a JavaScript object to JSON; how to transfer JSON in the Request body and access it via Request.InputStream; and the use of Json.Decode<T> to convert JSON to a type that your C# code understands and can work with.


Date Posted:
Last Updated:
Posted by:
Total Views to date: 96464


- Mauro Cesar Otoni

Thanks for the tutorial, but the download link does not work.

I would like if possible to receive an example on how to update data via JSON WebGrid, jquery.
The User enters the name into a text box, and WebGrid is updated with the database to JSON.

I am Brazilian, and please excuse me bad English.

- henry

Hi Mike
I am unable to download the sample site. Can you please test it and let me know.
Thank you for your great articles and effort in general!

- alex

hi how i start this project in visual studio

- Mike


If you want to create a Web Pages site in Visual Studio, you need the New Web Site option, not the New Project option.

Recent Comments

Gavan McGregor 09/10/2015 13:45
In response to Downloading multiple files in ASP.NET
Hi, (long time reader, first time poster!) Although it's a deliberately simple example, It's /...

Rodrigo 08/10/2015 20:40
In response to MVC 5 with EF 6 in Visual Basic - Creating an Entity Framework Data Model

Warren Caulton 08/10/2015 20:18
In response to Solved - The Microsoft.ACE.OLEDB.12.0 provider is not registered on the local machine
I have a Web Page that uploads and parses through an excel file to upload the latest sheets data to...

Vaclav Elias 07/10/2015 17:26
In response to ASP.NET 5: Dependency Injection and Services in MVC
Hi Mike, this is very nicely explained and elaborated from non dependency to dependency on real I am...

Nizen 07/10/2015 15:07
In response to Bind Data From a SqlDataSource to a Label
Wow thank you very much! I was really stuck and now I can be able to proceed coding as a result of...

Manolo 06/10/2015 21:15
In response to ASP.NET MVC Uploading and Downloading Files
Muchas gracias por esta información, Felicitaciones por su trabajo , me ayudó mucho , Saludos desde...

adam 05/10/2015 14:35
In response to Integrating Web API with ASP.NET Razor Web Pages
Can you re-open this web api project in webmatrix, once you've added web api? Basically I'm looking...

nish 24/09/2015 18:48
In response to Managing Checkboxes And Radios In ASP.NET Razor Web Pages
Very Interresting stuff! it really helped me to send an int value by checking a checkbox!...

Uğur Dinç 24/09/2015 16:45
In response to Scheduled Tasks In ASP.NET With Quartz.Net
Simplest and best explanation on Quartz.NET. Thank you!...

woo 24/09/2015 15:34
In response to Implementing Google's EU End User Consent Policy
Is there any way for the banner to appear only to EU visitors? I am referring to the jQuery code...