How to perform Insert/Update/Delete on EDMX Entities across Many-to-Many or 1-to-Many relation (using ADO.NET Data Service)? RRS feed

  • Question

  • Hello, I have a database with User, Role, and User_Role (join) tables. I successfully use the ADO.NET Entity Framework Wizard to generate the Model from the database, and the resulting EDMX diagram properly shows a many-to-many relationship between the User and Role table.  So this part is working ok.

    Then my Silverlight/C# client-side code is set up to where I have a Service Reference (Proxy) to the server-side ADO.NET Data Service which accesses the Entity Framework EDMX definitions (also on the server-side, along with the database). 

    I am able to successfully create a User or a Role to the database.  The problem comes when I try to ADD ROLES TO A USER, and THEN save that user to the database.  

    First, here is an example of what DOES work:

    Proxy.Entities proxy = new Proxy.Entities(new Uri("DataService.svc", UriKind.Relative));

    Proxy.User user = new Proxy.User();
    user.Username = UsernameTB.Text;
    user.FirstName = FirstNameTB.Text;
    user.LastName = LastNameTB.Text;
    user.CreatedDate = DateTime.Now;
    user.UpdatedDate = DateTime.Now;
    proxy.AddObject("User", user);


        (asyncResult) => { proxy.EndSaveChanges(asyncResult); },



    Now, here is an example of what DOES NOT work (note the lines where I say "selectedUser.Role.Add( )", and also note that "selectedUser" is a user that is already in the database and has been successfully queried/retrieved in another part of the client-side code):

    public UpdateUserRoles(Proxy.User selectedUser, List<Proxy.Role> roles)

        Proxy.Entities proxy = new Proxy.Entities(new Uri("DataService.svc", UriKind.Relative));

        proxy.AttachTo("User", selectedUser);






            (asyncResult) => { proxy.EndSaveChanges(asyncResult); },




    In this second example, I would expect the "User_Role" join table in the database to be automatically updated with the new User/Role entries (corresponding to adding specific Roles to a User).  In other words, there should be a new ROW in the User_Role join database table for every new Role I add to a User.  But for some reason, the User_Role join table does not get updated accordingly.  It remains blank, without any new ROWs added.  Furthermore, the same issue applies to tables which are related via a 1-to-many relationship.  Please let me know if you see something that I may be missing here.  Any help would be greatly appreciated.  Thank you.

    Thursday, October 16, 2008 7:36 AM


  • Hi,


    Adding to the collection property on the client will not add the links on the server.  You need to call DataServiceContext.AddLink() to add the links to the server.


    Hope this helps,



    Wednesday, November 5, 2008 8:51 PM