LINQ to SQL的许多一对多的关系没有第三类 [英] Linq to SQL many-to-many relationship without a third class
问题描述
在我的数据库,我有以下表格:
In my database I have the following tables:
- 客户(ID)
- 订单(ID)
- CustomersOrders(客户ID,订单ID)
我如何映射客户表,客户类别和订单表订单类,而无需创建一个类CustomersOrders?
How do I map Customers table to Customers class and Orders table to Orders class without creating a class CustomersOrders?
推荐答案
这取决于它LINQ的版本,你在说什么。
That depends on which LINQ version you're talking about.
如果您使用的是实体框架4.0,你必须在表中比其他的ID没有更多的信息,那么你所要求的应该已经产生。我相信同样是真正的实体框架1.0。
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的则是另一回事。它从来没有处理过许多一对多的关系很好。你必须让LINQ到SQL生成的第三个表,然后扩展部分类手在一个单独的文件,以掩盖走了第三个表。这是丑陋的,但它的工作原理。这里有一系列的博客文章,详细说明什么需要做的事情:
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:
<一个href="http://blogs.msdn.com/b/mitsu/archive/2007/06/21/how-to-implement-a-many-to-many-relationship-using-linq-to-sql.aspx">How使用LINQ to SQL来实现一个多一对多的关系
这篇关于LINQ to SQL的许多一对多的关系没有第三类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!