开发者

Linq to SQL many-to-many relationship without a third class

开发者 https://www.devze.com 2023-02-13 11:10 出处:网络
In my database I have the following tables: Customers (ID) Orders (ID) CustomersOrders (CustomerID, OrderID)

In my database I have the following tables:

  • Customers (ID)
  • Orders (ID)
  • CustomersOrders (CustomerID, OrderID)

How do I开发者_Go百科 map Customers table to Customers class and Orders table to Orders class without creating a class CustomersOrders?


That depends on which LINQ version you're talking about.

If you're using Entity Framework 4.0 and you have no additional information in the table other than the IDs then what you are asking for should already be generated. I believe the same is true for Entity Framework 1.0.

LINQ to SQL is another story. It never handled Many-to-Many relationships well. You have to allow LINQ to SQL generate the third table and then extend the partial classes by hand in a separate file to mask away that third table. It's ugly but it works. Here's a series of blog posts that detail exactly what needs to be done:

How to implement a many-to-many relationship using Linq to SQL

0

精彩评论

暂无评论...
验证码 换一张
取 消