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?