views:

275

answers:

2

I am currently working on a project utilising both the Entity Framework (the version packaged with the .NET Framework version 3.5 SP1) and lots of AJAX-based functionality, which I am implementing using jQuery on the client-side and ASP.NET WebMethods on the server-side.

I typically create new entities using a WebMethod that accepts the entity as a parameter as follows:

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

When calling this method, I assemble the object resembling the entity (in terms of properties) in JavaScript and pass it as a JSON string. However, when attempting to pass related entities in a many-to-many relationship I have encountered a problem. Say a User entity has a one-to-many relationship with the Flight entity - adding a new user with multiple (new) flights is proving problematic. The following approach:

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

Results in an ASP.NET error (at the deserialization stage) that states that List cannot be converted into EntityCollection.

I then tried this approach:

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

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

Does anyone have any idea how I can pass multiple Flights in an EntityCollection to a WebMethod?

+1  A: 

Why not just return the list of Flights as a second parameter, then add them to the User within your method?

I don't use [WebMethod] with EF parameters, and I generally pass POD structures as parameters, so I've never had this problem....

NVRAM
In the end I did end up passing it as a separate parameter and adding it as you say. This worked just fine, but I wondered if there was a "proper" (or at least more elegant) way of doing it.
CheeseRuinsLives
+2  A: 

You may have some difficulty using the built-in serializers while attempting to use your EF entity classes directly. Even if you were able to deserialize from a javascript array into an EntityCollection, you wouldn't be able to go the otherway (EF object graph to js obj): you'd get the famous "circular reference" error.

I have a lot of projects where I use EF and web services (but I haven't used the Dynamic Data stuff yet). There may be some other good options for you to try. You may be able to create DTO or POD classes ("Data Transfer Object" or "Plain Old Data") classes that represent all of the properties and relationships of your entity classes (but contain no methods or other code). Those can easily be serialized and deserialized and passed around over the wire. Though, you may still end up with circular reference errors. For those, you could possible do your own serialization using Json.NET, which supports circular references. Your services would have to be modified to return strings, and you'd have to evaluate the json string to an object literal at your javascript calling site. That's not too much work though.

You could possibly use DataSvcUtil.exe to create your DTO classes.

All that being said, I have a project set up where I am currently trying to solve your problem. I get the same error that you stated, and the same behavior you mentioned when switching from an [] to a {} in the javascript. I'll keep trying, and let you know if I find anything.

Samuel Meacham
Thanks for the possible solutions. As I posted above, I did get around it with a separate parameter, but if I have time I'll attempt one of your suggestions.I can't help but feel disappointed that it isn't simpler to do this - the basis appears to be there but there are too many niggling problems surrounding EF + JSON for my liking. Do let me know if you progress at all, though.
CheeseRuinsLives