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

时光总嘲笑我的痴心妄想 提交于 2019-12-10 19:47:54

问题


In my database I have the following tables:

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

How do I map Customers table to Customers class and Orders table to Orders class without creating a class CustomersOrders?


回答1:


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



来源:https://stackoverflow.com/questions/5092322/linq-to-sql-many-to-many-relationship-without-a-third-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!