简体   繁体   English

Linq to SQL多对多关系-插入更新和删除

[英]Linq to SQL many to many Relationships - Insert Update & Delete

I have a database that relies on a bunch of Many-to-Many and rather than denormalizing the whole database (which is quite large) I would like to know if there is a SIMPLE way of implementing Many-to-Many Relationships using LINQ-to-SQL Yet... 我有一个依赖一堆多对多数据库的数据库,而不是对整个数据库进行非规范化(这相当大),我想知道是否存在使用LINQ-实现多对多关系的简单方法to-SQL还...

Everywhere I look I find articles on HOW Difficult It is, or A Simple Workaround that is actually "NOT" Simple. 我到处都可以找到有关“如何困难”的文章,或者实际上是“不”简单的简单变通方法。 But all the articles I have been able to locate are pretty dated, using asp.net 3.5. 但是使用asp.net 3.5,我能够找到的所有文章都过时了。

Should I just Create a Stored Procedure and then Drag it into the .DBML file as a method? 我应该只是创建一个存储过程,然后将其作为一种方法拖到.DBML文件中吗?

So I guess my question is: 所以我想我的问题是:

Is there any new way to implement many-to-many using Linq-to-Sql in ASP.NET 4.0 along with MVC 3. 是否有任何新方法可以在MVC 3和ASP.NET 4.0中使用Linq-to-Sql实现多对多。


Examples of usage in my Project: 我的项目中的用法示例:

数据库语言

In the finished model, there will be Other Tables linked to the Many-to-Many Tables (ie EventAddress, EventUrl, VipAddress, VipPicture, GuestAddress, GuestPhone, GuestEmail, etc...) 在完成的模型中,将有其他表链接到多对多表(即EventAddress,EventUrl,VipAddress,VipPicture,GuestAddress,GuestPhone,GuestEmail等)。

So you can see when this would be a pain in the a$$ to denormalize. 因此,您可以看到何时需要对a $$进行非规范化。

Thank you, Tim 谢谢蒂姆

Have you considered using Entity Framework? 您是否考虑过使用实体框架? It handles m:n relationships nicely. 它很好地处理了m:n关系。 In your case the EmployeeAddress table would be abstracted away as a list of addresses on your Employee object. 在您的情况下,EmployeeAddress表将被抽象为Employee对象上的地址列表。 It is also my impression that linq to sql isn't really a priority for Microsoft, EF Is. 我的印象也是,LINQ to sql并不是微软EF Is的真正优先事项。 I understand if you do not wish to convert an existing project, but for new projects EF4 would probably be a better fit. 我了解您是否不希望转换现有项目,但是对于新项目,EF4可能更合适。

Although you can map many-to-many relationships in both LINQ to SQL and LINQ to Entities, LINQ to Entities allows a direct mapping of many-to-many relationships with no intermediary class, while LINQ to SQL requires that an intermediary class map one-to-many to each of the classes that are party to the many-to-many relationship. 尽管您可以在LINQ to SQL和LINQ to Entities中都映射多对多关系,但是LINQ to Entities允许没有中间类的直接对多关系的映射,而LINQ to SQL要求中间类映射一个多对多关系的每个类的“一对多”。

link to the quote above - http://msdn.microsoft.com/en-us/library/cc161164.aspx 链接到上面的报价-http://msdn.microsoft.com/zh-cn/library/cc161164.aspx

LINQ to SQL will always map directly to the structure of the database. LINQ to SQL将始终直接映射到数据库的结构。 If your database has all of those junction tables, then they will be in your LINQ to SQL model - period. 如果您的数据库具有所有这些联结表,则它们将在LINQ to SQL模型中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM