Circular Reference and ASP.NET

347 views
Skip to first unread message

Nate

unread,
Jun 7, 2011, 1:29:46 PM6/7/11
to KnockoutJS
I wanted to check with everyone here and see how they dealt with the
circular reference error generated when using Linq to SQL or Entity
framework. I followed the solution at this link and it seems to work
so far but I wanted to make sure there wasn't a better solution to
this problem.

Solution I Used: http://www.west-wind.com/weblog/posts/2007/Sep/02/LINQ-to-SQL-and-Serialization


Thanks

ERROR MESSAGE: A circular reference was detected while serializing an
object of type

Jeff Sheldon

unread,
Jun 7, 2011, 1:59:23 PM6/7/11
to knock...@googlegroups.com
I avoid passing my database objects down to my view.

I map (Using AutoMapper for complex stuff) to view models or Dto's and pass them down instead.

Jeff Espenschied

unread,
Jun 7, 2011, 4:16:48 PM6/7/11
to knock...@googlegroups.com
I use newtonsoft json instead of the framework serializer and configure it to ignore circular references. E.g. new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore };

-- Jeff

Nate

unread,
Jun 8, 2011, 12:46:00 AM6/8/11
to KnockoutJS
Great Jeff! This seems better than touching the Entity model.

Thanks!

Andrew Booth

unread,
Jun 8, 2011, 10:00:41 AM6/8/11
to knock...@googlegroups.com

Nate

unread,
Jun 21, 2011, 3:58:21 PM6/21/11
to KnockoutJS
How I solved Circular reference issue.
Since we do not want the collections that are added as a navigation
properties, the simplest thing to do was to copy the data into array.
Simply create a collection of the data you want.

public List<Order> GetOrders()
{
List<Order> orderList = new List<Order>();

var OrderQuery = from o in this.Context.Orders
select o;

foreach (var order in OrderQuery)
{
Order newOrder = new Order();

newOrder.OrderId = order.OrderId;
newOrder.Value = order.Value;

orderList.Add(newOrder);

}

return orderList;
}




On Jun 7, 12:29 pm, Nate <southpointt...@gmail.com> wrote:
> I wanted to check with everyone here and see how they dealt with thecircularreferenceerror generated when using Linq to SQL or Entity
> framework. I followed the solution at this link and it seems to work
> so far but I wanted to make sure there wasn't a better solution to
> this problem.
>
> Solution I Used:http://www.west-wind.com/weblog/posts/2007/Sep/02/LINQ-to-SQL-and-Ser...
>
> Thanks
>
> ERROR MESSAGE: Acircularreferencewas detected while serializing an
> object of type
Reply all
Reply to author
Forward
0 new messages