I'm writing an OData / Entity Framework client server app, and can't get Many to Many relationships to update from the client to the server.
The OData server is written using VS2013 in c# using WCF 5.6, and Entity Framework 6.
The database is simply 3 tables, Users, Permissions and a joining table UsersPermissions.
I'm using Entity Framework 6, which abstracts away the joining UsersPermissions table, leaving just the Users and Permissions entities.
From a User I can query their permissions no problem from a client using LINQ. Similarly I can query Users from Permissions, so the many to many relationship is good for reading from.
If I try to create a relationship from the client, say adding a new permission for a user, nothing happens. No traffic to the server, no data is changed in the joining table in the database.
This is the same whether I try to add a permission to a user in my client application, or if I use LinqPad to execute the query against my OData source.
The LINQ I'm using (taken from LinqPad) is:
// Get permission var Permission = (from p in Permissions where p.PermissionId == 1 select p).SingleOrDefault(); // Get user var User = (from u in Users where u.UserId == 1 select u).SingleOrDefault(); // Add permission to user User.Permissions.Add(Permission); SaveChanges();
I have tried creating a completely separate OData server application, and get the same problem.
If I execute the above LINQ within the OData server app, it executes as expected and the entry is added to UsersPermissions.
So it seems that the problem only exists when trying to update a many to many relationship from an OData client?
There is mention at the end of this article that OData has a bug in refreshing many to many relationships, but I can't even create one! http://msdn.microsoft.com/en-us/library/vstudio/bb896317(v=vs.100).aspx
I'd appreciate any help or suggestions towards tracking down why I cant create a many to many relationship from an OData client against an OData server using EF6.0.
To add a relationship between user and permission on client side, Please use DataServiceContext.AddLink
dsc.AddLink(User, "Permissions", Permission) dsc.SaveChanges()