views:

30

answers:

1

I have just updated to using EF 4.0 where before i was using Linq 2 SQL.

I have a query:

    var UserList = this.repository.GetUsers();
    return Json(UserList, JsonRequestBehavior.AllowGet);

This was generating an error: "A circular reference was detected while serializing an object of type"

This prompted this code which worked fine in L2S:

  var UserList = this.repository.GetUsers();
      foreach (User u in UserList){
          u.Subscriptions = null;
      }
  return Json(UserList, JsonRequestBehavior.AllowGet);

How can i stop EF from looking into the Subscriptions table, i just want the Userlist, none of the related properties and the above example does not seem to work for this.

Cheers, Kohan

+1  A: 

Project your UserList before you pass it to the Json serializer so that it doesn't dive into any of the EF generated properties.

var UserList = this.repository.GetUsers().Select(user => new {Name = user.Name, Email = user.Email, ...);
Hightechrider
Of course :) - Thanks for that. Job done.
Kohan