Loading Entity Framework 4.0 POCO classes can still cause circular references

I’ve been designing my MVC apps using custom ObjectContext’s and POCO style classes.

So, to my surprise, when serializing a lazy loaded POCO class to a JsonResult, I stumbled onto this.

“A circular reference was detected while serializing an object of type…”

One potential way to get around this, set the following in your custom ObjectContext

this.ContextOptions.ProxyCreationEnabled = false;

ASP.Net MVC and dynamic flexigrid quick example

I’ve ┬ájust been playing around with Flexigrid for jquery.

Here’s a simple example on how to structure the json data in ASP.Net.

Flexigrid needs a json data format like this:

{
	"page":1,
	"total":30,
	"rows":
	[
		{"id":"1","cell":["16/12/2010 16:03:45","test","system"]},
		{"id":"2","cell":["16/12/2010 16:04:45","test","system"]}
	]
}

To generate that format in c# using LINQ, simply do something this:

var logs = service.LogList();

var rows = from l in vm.Logs
select new
{
	id = l.LogId.ToString(),
	cell = new List
	{
		l.LoggedDateTime.Value.ToString(),
		l.Message,
		l.System
	}
};

var data = new
{
	page = 1,
	total = logs.Count(),
	rows = rows
};

JsonResult jr = new JsonResult { Data = data, JsonRequestBehavior = JsonRequestBehavior.AllowGet };