Entity framework EntityCollection and DataContractJsonSerializer - by Ido Flatow. _

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 354859 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 7/3/2008 6:56:40 AM
Access Restriction Public



I have a model that holds 2 related entities - Person and Addresses (multiple addresses per person).

When I load a person with his addresses and serialize them using DataContractJsonSerializer, the json string only holds the Person data and not the Addresses data.

If I try to use Asp.Net Ajax JavaScriptSerializer, I am able to serialize both the person and the addressed, but if I try to deserialize it with DataContractJsonSerializer, I only get the Person and not the Addresses.

From what I found, the reason is that the Addresses property doesn't have a DataMemberAttribute and doesn't have a set part for the property.

I tried to add the attribute and the set code to the generated code (manually adding code that takes the set value and add it to the EntityCollection), and managed to make the serializer work, which made me to believe that there is a bug in the way Entity frameworks generates the collection properties.

I hope it can be fixed soon,

Ido Flatow.
Sign in to post a comment.
Posted by Microsoft on 10/28/2008 at 3:21 PM
Thank you for submitting this very valid piece of feedback. The lack of DataContractJsonSerializer support for Entity types is certainly an important feature hole, and we will certainly be investigating possible solutions in future versions. Unfortunately, the current serialization model only allows one projection per type, EDM type generation only allows generating IsReference=true types, and there is no standard way of encoding references in JSON. Clearly one of these 3 constraints has to give for this to be solved, but all have problems. Multiple projections would be great, but may be complex to understand/use and would be a major new feature (it is on our radar for the future). Generating non-IsReference types would mean abandoning double-links (e.g. if a Product has many Parts, than either Product would have to not have a Parts data member, or Part would have to not have a Product data member) - this makes it non-straightforward to reason about the serialization of these types (you may need Product-->Parts link in one scenario, but Part-->Product link in another - so which do we choose at generation time?). The third component (JSON format for references) is not possible without an industry-wide standartization effort or a Microsoft-specific format. Rest assured that even given these difficulties, we're looking into solutions on all 3 fronts. For now, I am happy that you found a workaround using the AJAX serializer that seems to be working for you - thank you for your patience while we're working to make this better.
-- Eugene Osovetsky, Program Manager, Connected Framework Client
Posted by Ido Flatow. _ on 9/29/2008 at 2:19 AM
AJAX serializer doesn't automatically support serializing these entities. Yes, it does support IsReference, but the serialization fails on serializing 1:1 relations, because EntityReference contains an object of the AssociationType and that object cannot be serialized because of a circular reference.

There is a workaround for it, but not a straight-forward one.

Will there be no solution for using EF, WCF and JSON? and I don't consider Data Services as an option because of it's lack of security
Posted by Microsoft on 9/18/2008 at 10:03 AM
In the released version of the entity framework in VS 2008 SP1, an entity is marked with DataContract(IsReference=true), which will cause reference tracking when the entity is serialized with WCF. The DataMember attribute is on all scalar and relationship properties.

The issue you are running into is that the DataContractJsonSerializer does support the IsReference flag and so cannot reference track. You can use the AJAX serializer which does support IsReference.
Posted by Ido Flatow. _ on 7/23/2008 at 2:16 AM

The problem seems to manifest itself in another place - WCF service with json support (which is logical because of the use with DataContractJsonSerializer).

When I create a WCF service that has an endpoint of webHttpBinding and the service
returns an Entity Framework object, the AJAX code in the client side fails because the return message (the http response) is empty - not even an http header, just a blank message.

I've tried the answer given to me in the MSDN forums
(http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=3569648&SiteID=1), but apparently changing the IsReference to false causes the c# code to crash because the base class "EntityObject" that all entities derive from, has the IsReference set to true !
Posted by Microsoft on 7/4/2008 at 3:03 AM
Thanks for your feedback. We are escalating this bug to the product unit who works on that specific feature area. The team will review this issue and make a decision on whether they will fix it or not for the next release.

Thank you,
Visual Studio Product Team
Posted by Ido Flatow. _ on 7/4/2008 at 12:56 AM
By checking the EntityFramework code using Reflector, I noticed that the DataMember attribute is not applied to collection properties. Is there a reason for doing so? if so, what is the correct way to serialize both the parent and his children all at once?