Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to Sql - Many to many - CRUD

Tags:

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

like image 772
alexn Avatar asked Aug 24 '09 12:08

alexn


1 Answers

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

like image 172
Justin Niessner Avatar answered Nov 15 '22 14:11

Justin Niessner