Adding Entity Framework Entities (One to Many) to JSON via ASP.NET WebMethod

I am currently working on a project using both the Entity Framework (a version integrated with the .NET Framework version 3.5 SP1) and many AJAX-based functions that I implement using jQuery on the client side and ASP.NET WebMethods on the server side .

I usually create new entities using WebMethod, which takes an object as a parameter as follows:

[WebMethod] public static void Add(User user); 

When calling this method, I collect an object similar to the object (in terms of properties) in JavaScript, and pass it as a JSON string. However, when I tried to convey related objects in a many-to-many relationship, I ran into a problem. Suppose a user-defined object has a one-to-many relationship with a Flight object — adding a new user with several (new) flights is problematic. The following approach:

  this.user.Flight = []; var x = new Object(); // Code here to populate x properties this.user.Flight.push(x); // Repeat again for more flights 

Results from an ASP.NET error (during the deserialization phase), which indicates that the List cannot be converted to an EntityCollection.

Then I tried this approach:

  this.user.Flight = {}; var x = new Object(); // Code here to populate x properties this.user.Flight.firstFlight = x; // Repeat again for more flights 

In this case, WebMethod was called successfully, but the flights were completely lost (calling the user.Flight.Count () function in the returned WebMethod 0).

Does anyone know how I can transfer multiple flights to EntityCollection in WebMethod?

+4
source share
2 answers

You may have difficulty using the built-in serializers when trying to use the EF object classes directly. Even if you could deserialize from a javascript array to an EntityCollection, you would not be able to go the other way (EF object diagram in js obj): you would get a known "circular link" error.

I have many projects where I use EF and web services (but so far I have not used Dynamic Data). There may be some other good options for you to try. You can create DTO or POD classes (Data Transfer or Normal Old Data classes) that represent all the properties and relationships of your object classes (but do not contain methods or other code). They can be easily serialized and deserialized and walked around the wire. Although, you can still get circular control errors. For them, you can do your own serialization using Json.NET , which supports circular references. Your services must be modified to return strings, and you will need to evaluate the json string for the object literal on your javascript call site. It doesn't work too much.

You can use DataSvcUtil.exe to create your own DTO classes.

All that has been said, I have a project where I am trying to solve your problem right now. I get the same error you were talking about, and the same behavior you were talking about when switching from [] to {} in javascript. I will continue to try and let me know if I can find anything.

+2
source

Why not just return the flight list as a second parameter, and then add them to the user as part of your method?

I do not use [WebMethod] with EF parameters, and I generally pass POD structures as parameters, so I never had this problem ....

+1
source

All Articles