Archive

Posts Tagged ‘mvc’

Entity Framework Objects with Proxies, MVC4, and Telerik Grids.

August 8th, 2012 No comments

I’m currently working on a side project that is using MVC4 and EF5. Some of the EF objects have bi-directional navigation on them due to how they’ll end up being rendered.

I was trying to wire up a Telerik MVC grid for data entry and received this error on the grid select:“A circular reference was detected while serializing an object of type ‘System.Data.Entity.DynamicProxies.CourseType”.

After some googling, the easiest answer I found was to disable proxy generation. This was fine as I wasn’t using lazy loading. Once I did that, I started getting this exception: “The RelationshipManager object could not be serialized. This type of object cannot be serialized when the RelationshipManager belongs to an entity object that does not implement IEntityWithRelationships.”. The easy answer didn’t work.

At first I tried to set up the Json serialization settings of the global formatter since MVC4 uses JSON.Net by default.

That did not solve the problem.

I then found a code sample on Telerik’s site that accomplished the goal, but it seemed overly complex (it may have just been the only way to solve it in mvc3). Based on that code sample, and using JSON.Net, this is what I came up with as a final solution:

Update: For this to work, Proxy generation still needs to be disabled. I didn’t make that clear in the original text.

Categories: Coding Tags: ,