views:

480

answers:

1

Hi!

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

+4  A: 

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

Justin Niessner
Yeah, that's how i do my selects at the moment. Thanks, waiting for sample!
alexn
Thanks for your answer, i've tried this approach and it work OK.
alexn