M2M with Explicit Join

Apr 2, 2012 at 3:23 PM

I really need to have support for M2M with explicit join tables.  The sample code readme says it only demonstrates M2M without the explicit join table. Many to 1 is already supported by RIA but M2M without explicit join means that I have to repeat the same string or other data in child tables which wastes disk space.

I want to make sure that I understand that the M2M project does not support joins with an explicit join table and that the mentioning of this on the web page was not just for tutorial purposes. Correct?

Coordinator
Apr 14, 2012 at 7:40 PM

Hi MilliMonk,

I'm very sorry for my late response, and I hope that my response is still relevant for you...

What you want is possible, but there is no tool support for it. This means you've to add the necessary code yourself. If you look at Figure 3 on the m2m4ria website, you only need to have create the M2MView in the upper half of the picture. This can be realized as follows:

  1. You need to copy the IEntityCollection and interface EntityCollection class from the sample code (this is from the file M2M4RiaDomainContext.g.cs in the project M2M4RiaDemo)
  2. You have to extend your join type classes in your Silverlight project (these are the join type classes generated by RIA services). You can have a look at the DogTrainer class (also contained in the file M2M4RiaDomainContext.g.cs in the project M2M4RiaDemo) and add corresponding code to your join type class.
  3. You have to extend both classes of the m2m relation in your Silverlight client (again, these are the classes generated by RIA services). You can have a look at the Dog and Trainer classes (in the file M2M4RiaDomainContext.g.cs in the project M2M4RiaDemo) and add corresponding code to your classes.

After doing this you've, created a many-2-many view that shields away the underlying join type classes and corresponding 1-n relations (although both, the joint type classes and the 1-n relations are still present). Since it is simply a view, there is no magic going on, and for RIA services nothing changes. You might want, however, remove the Query operations for your join types in your domain service because they are no longer needed, unless there is a need to explicitly query your join type entities.

In your case there is one aspect that may cause problems. In case your join type classes have additional mandatory/non-nullable properties, you can't create new m2m associations between 2 objects, because the join type object that is created under water won't have these properties filled in and saving your data to the data base will than fail.

I hope this explanation makes sense to you and is of any help.

Feel free to contact me in case of problems/questions.

 

kind regards,

Merijn