This is a migrated thread and some comments may be shown as answers.

[Solved] Entity Framework, JavaScriptSerializer, Circular References Detected

9 Answers 223 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Kevin
Top achievements
Rank 1
Kevin asked on 25 May 2011, 02:13 PM
I'm running the latest version of the Telerik ASP .NET MVC Extensions, using Entity Framework Code First 4.1 for data access, and using ASP .NET MVC 2 for the application.

When I was passing LINQ to Entities queries directly to the model for the grid it would only work properly with server binding.  Any attempt to add AJAX support resulted in an exception from the JavaScriptSerializer complaining about circular references.  After much research I've come to find that this is most likely a limitation of the JavaScriptSerializer and not related to the Telerik controls.

Following advice on Resolving circular references when binding the MVC Grid I created a set of view models to map the entities to something the JavaScriptSerializer can work with.  Now the problem is that sorting, paging, filtering, and so on are all broken.

What is the proper way to pass data into the model for the grid to consume using the setup described above?  More information any samples of my classes (they're super simple...) can be found on my related Stack Overflow post: Entity Framework Code First Dynamic Proxy Navigation Properties.

9 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 25 May 2011, 02:16 PM
Hello Kevin,

 What do you mean by saying that sorting and paging are broken? Is there chance to attach a sample project so we can investigate?

 The grid should work as expected with view models provided it is setup as in the blog post.

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Kevin
Top achievements
Rank 1
answered on 25 May 2011, 02:24 PM
private IEnumerable<Member> DoNotContactList {
    get {
        return from member in this.Entities.MemberRepository.DoNotContact
               select new Member(member);
    }
}

In the above, the property DoNotContact of MemberRepository returns the EF IQueryable.  The new Member(member) is mapping the EF entity to a view model of the same name.

If you look at the comments on the blog post about using view models, Makoto writes:

"With the ViewModel approach, won't you lose the built in Linq expression engine?

Wouldn't we have to implement our own custom paging/sorting?

That's what I'm running into at least. If I don't implement my own paging/sorting, then my application queries for EVERY record in the database."

That's exactly the problem I'm having now.  The exception I get is "The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'."
0
Atanas Korchev
Telerik team
answered on 25 May 2011, 02:27 PM
Hi Kevin,

 This looks like a different problem. EF needs OrderBy to be issues before Skip(). The grid has some code which checks for EF and emits an OrderBy if there isn't one. Which version of Telerik Extensions for ASP.NET MVC are you using? We had a problem with CodeFirst EF which I think we have addressed.

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Kevin
Top achievements
Rank 1
answered on 25 May 2011, 02:29 PM
The version of Telerik.Web.Mvc is 2011.1.315.235.
0
Atanas Korchev
Telerik team
answered on 25 May 2011, 02:45 PM
Hello Kevin,

 Could you please try with the attached build? If it still fails - please send me your project. I think this is not a problem with the view model approach but a known issue which we currently do not detect and circumvent.

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Kevin
Top achievements
Rank 1
answered on 25 May 2011, 03:16 PM
Thanks for the updated code.  Unfortunately, I'm still having the same error during AJAX paging or sorting - Skip must be called after OrderBy on LINQ to Entities.

I've tried applying the paging and sorting before passing the model into the grid control.  The problem with this is that the grid control then thinks the data set is only as large as the page I've given it.

Is there any other way around this?
0
Atanas Korchev
Telerik team
answered on 25 May 2011, 03:22 PM
Hello Kevin,

 If you don't want to send us your project then you can try implementing custom binding as shown here.

Greetings,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Kevin
Top achievements
Rank 1
answered on 25 May 2011, 03:58 PM
Due to confidentiality of my client I can't share the project.  Custom binding appears that it will be workable using view models.  I still can't use any EFCF results directly or I immediately get circular reference exceptions.

Thanks, I suppose I'll have to go this route for now.  I appreciate your very prompt responses to my questions.
0
nack
Top achievements
Rank 1
answered on 30 Jun 2011, 03:15 PM
Its because Microsoft don;t actually test their products in real world scenarios. If they did they would know that what everyone is trying to do is impossible with their technologies. For instance, i tried first to use the POCO stuff with proxies, only to find the proxies are not serializable.  Then i found the ProxyDatacontractResolver (no help for the XmlSerialixer of coursre) and that worked. But then i found the JavaScriptSeralizer couldn;t work with the circ references too like yourself.  I don't think they set out to even understand the common scenarios and each team just hacks away at various things  until enough people shout out what the rest of the world knows - hey we want to serialize our data to JSON AND XML AND ANYTHING WE WANT INCUDING AN OPTION TO CUT CIRC REFERECES DEAD ON DETECTION UNLESS IS SUPPORTED BY RECEIVER FORMAT.

Falls on deaf ears because thwy unfortunatley don;t seem to build stuff with their own technologies - they just sell it - so they only find out the issues later on.
Tags
Grid
Asked by
Kevin
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Kevin
Top achievements
Rank 1
nack
Top achievements
Rank 1
Share this question
or