I have a Silverlight application which uses ADO.NET Data Services and the Entity Framework to interact with a SQL Server Database. In the SQL Server database I have: User, Role, and User_Role (join) tables. I successfully use the ADO.NET Entity Framework Wizard to generate the Model from the database in my Silverlight application server-side code (.NET), 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 in my Silverlight C# code:
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);
proxy.BeginSaveChanges(
SaveChangesOptions.None,
(asyncResult) => { proxy.EndSaveChanges(async
Result); },
null
);
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);
selectedUser.Role.Add(role
s[0]);
selectedUser.Role.Add(role
s[1]);
selectedUser.Role.Add(role
s[2]);
proxy.UpdateObject(selecte
dUser);
proxy.BeginSaveChanges(
SaveChangesOptions.Batch,
(asyncResult) => { proxy.EndSaveChanges(async
Result); },
null
);
}
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.