I'm currently using Linq to sql as my OR-mapper. My problem is that i can't come up with a way to do crud-operations on a many-to-many context.
The read part is no problem. I just create a partial class and expose a property that reads all entries using my relation table.
What is the best way to add Create, Update and Delete function to this?
Thanks
Unfortunately, LINQ to SQL doesn't handle many to many relationships gracefully.
The nice thing is, that you can add custom code to the generated code to make things perform much nicer. By adding custom collections to your generated objects, you can make the handling of many-to-many relationships transparent.
Here's the link to the blog that got me started. My code is more complicated (implements IBindingList, etc.) but this gives you the basic idea:
How to implement many-to-many relationships using Linq to SQL: Part II (includes add/remove support)
And here's a link to the source code for that project:
Source Code
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With