I'm doing some tests to get a taste of object-relational mapping using LINQ to SQL and I've got stuck trying to make a many-to-many relationship work. To keep it simple, suppose I have an Order class that owns a collection of ordered Products. How can I map this product collection using LINQ to SQL?
[Table(Name = "OrderTable")]
public class Order
{
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public int ID;
[Column]
public string Customer;
[Association(ThisKey = "ID", OtherKey = "ID")] // Not working
public EntitySet<Product> products = new EntitySet<Product>();
}
[Table(Name = "ProductTable")]
public class Product
{
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public int ID;
[Column]
public string Name;
[Column]
public double Price;
}
I tried using an EntitySet as a container to hold an Order's Products but it didn't work because LINQ queries are returning Order objects with empty products collections.
PS: From This question it seems that I need to create a third class to keep track of Order-Product relations, but I was hoping to find a way of doing it without having to create another class.