Jon Miller

It's been awhile since I've looked at what was previously known as DLinq. In the May CTP, there wasn't a many-to-many mapping. i.e. you could do it, but, you had to use one-to-manys and map the link table as a class. Can someone tell me if there is a true many-to-many mapping now



Re: LINQ Project General Is there a many-to-many relationship yet?

Klaus Even Enevoldsen DK

Jon,

There is still no Many-To-Many associations in LINQ To SQL (DLINQ), from what I understand it will not be implemented in version 1, if it will appear in a later release I don't know.

/Klaus Enevoldsen





Re: LINQ Project General Is there a many-to-many relationship yet?

David Buchanan

I can't wait to get back to my LINQ environment to try this out:

http://www.codeproject.com/cs/database/LINQ_many2many.asp

One thing I didn't understand about this article... doesn't "Movie" have a this.DataContext Oh, the article is written on the May2006 preview... maybe that property didn't exist back then. (maybe it doesn't exist now... I could have imagined it).





Re: LINQ Project General Is there a many-to-many relationship yet?

Jon Miller

I think you have to use LINQ to Entities to get a true many-to-many. Unless the link tables have other columns of interest, having a class that represents them makes no sense IMHO. It should work the way it does with other ORMs.

Jon





Re: LINQ Project General Is there a many-to-many relationship yet?

David Buchanan

That's an interesting opinion. Here's some code:

using System.Data.Linq;

namespace DDBSandboxApp

{

public partial class Supplier

{

protected EntitySet<Category> _categories;

public EntitySet<Category> Categories

{

get

{

_categories = new EntitySet<Category>(onAddCategory, onRemoveCategory);

foreach (Product MyProduct in this.Products)

{

_categories.Add(MyProduct.Category);

}

return _categories;

}

}

protected void onAddCategory(Category arg)

{

//might want to put the right thing here

}

protected void onRemoveCategory(Category arg)

{

//might want to put the right thing here

}

}

}

To use it, here's some more code:

public static void UseDataModelExtension()

{

SandboxDataContext MyDB = new SandboxDataContext();

Supplier MySupplier = MyDB.Suppliers.Take(1).SingleOrDefault();

Console.WriteLine(MySupplier.CompanyName);

foreach (Category MyCategory in MySupplier.Categories)

{

Console.WriteLine(" " + MyCategory.CategoryName);

}

}

The data is arranged like this: Supplier -< Product >- Category... or in English: Supplier is related many-to-many to Category through Product.

The first code extends Supplier to add a property which represents all of its related Categories. This property is populated through existing mappings. I didn't have to specify any this-ID-goes-to-that-ID stuff.

Yes, one would have to write another Extension to Category to pull the related Suppliers. It's not that hard.

I'm not crystal clear on the purpose of the onAdd and onRemove methods and one should figure out what those are for before trying this stuff in production. They aren't needed for fetching.

When I run the second code against NorthWind, I get:

Exotic Liquids
Beverages
Condiments

Is this stuff cool or what





Re: LINQ Project General Is there a many-to-many relationship yet?

David Buchanan

Oh, and just so I'm first to say it:

Above I'm traversing two associations.

This technique could be used to traverse n associations.

That means if you have States -< Hospitals >-< Doctors >-< Patients >-< BreakfastHistory, you could add a Property to States which gets the BreakfastHistory of all the Patients of Doctors who have ever worked at the Hospitals in the State. Programmers could use your Property without knowledge of the physical implementation of the real association between States and BreakfastHistory.

All this and you don't have to restate the associations that are already present in the database. You just use those associations through the properties already provided.

Totally Cool.





Re: LINQ Project General Is there a many-to-many relationship yet?

Matt Warren - MSFT

There are no natual many-to-many associations in LINQ to SQL (in V1). You must expose the intermediate table with two one-to-many associations.




Re: LINQ Project General Is there a many-to-many relationship yet?

Galin Iliev

take a look at the article on code project Create many-to-many relationships in LINQ for SQL

David,

in sample I exposed DataContext through Program class because there was not this.DataContext in May 2006 Preview

hope this helps

P.S What do you think about the article Any feedback is highly appreciated.