开发者

Circular references not allowed - Json serialization

I am in midst of developing an application using Entity Framework code first 4.1 and MVC3. Here are three entities that I have, basically, State => City => Locality.

public class State {
    public virtual List<City> Cities { get; set; }
}

public class City {
    public virtual List<Locality> Localities { get; set; }
    public virtual State State { get; set; }
}

public class Locality {
    public virtual City City { get; set; }
}

It can be seen that I am using bi-directional relationship for all three entities. Json does not allow that, so I am somewhat frustrated. Basically开发者_Python百科, I need to navigate in either direction. For example given a City I want to be able to locate State to which it belongs easily.

I scanned the web and came across a couple of workarounds, but none of them suits my scenario. The first was to make the relationship unidirectional(who will do that !) and I don't want that. Other was to introduce ViewModel with only the properties that I need, but then that would mean duplicate code if I need to use all the fields of Entity regularly. Also my controller will be flooded with those properties. So I don't like that too.

I was thinking that this was just basic stuff, but now I am struggling to find a workable solution. If anyone has a better alternative(perhaps something in MVC3), please help me out.


You could try using JavaScriptSerializer directly and registering your own converter to control the serialization process.


I am not an ASP expert, but I think the solution might be similiar to what I woudl do in Java, Groovy or python or any other language.

The best solution I could propose is to make City.Localities a kind of "transient" (in Java terms) field - i.e. don't serialize it, but update it at loading time (when you build the structure). This can be encapsulated in setter for City of the Locality class. So in Locality.setCity, in the set method you should call (city->localities.append(this) (whatever language you write it in). This way it will become a "runtime cache" of City->Localities which will be build once during loading.


The problem appears to be a native issue with the DataContractJsonSerializer support for Entity types. In short, Entities that have relationships (i.e. two-way) with other Entity types cannot be serialized through Json. In your example: a State table connected to a Cities table will not translate well into Json serializing because a State may have many cities and a City is associated with a State.

One quick solution would be anonymous object projection. Something like this example:

public JsonResult Orders()
    var results = from Ord in databaseContext.Orders
          select new
          {
              OrderID = Ord.ID,
              OrderTitle = Ord.Name,
              OrderDate = Ord.OrderDate
          }

    return Json(results);
}

For reference, see this: Serializing Entity Framework Objects into JSON Using ASP.Net MVC

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜