在EF 4.0中缺少多对多链接表的错误或功能? [英] Lack of a many-to-many linking table in EF 4.0 a bug or a feature?

查看:80
本文介绍了在EF 4.0中缺少多对多链接表的错误或功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个评论和一个问题。实体框架4.0不会在两个表之间的多对多关系中显示链接表,例如在Northwind中显示的Order_Details,订单和产品之间的链接表,如果仅使用两个主键,则使用两个主键在连接表中,通常情况。所以在Northwind,如果您在订单和产品之间的Order_Details链接表中使用OrderID和ProductID作为主键,对于多对多关系,如果仅存在这两列(主键),链接表将不会显示在Order_Details中。



因此,您无法在多对多关系链接表中插入或创建,因为Entity Framework 4.0不显示链接表Order_Details或Intellisense显示这个链接表的许多关系。如果EF 4没有显示隐藏的链接表,那么如何在链接表中插入或更新呢?诸如你现在开始在OOP中思考这样的论据不会让我印象深刻。 SQL有一定的结构,OOP只是一个接口,所以我们可以使用LINQ到实体,而不是笨重的SQL查询,IMO。



获取围绕这个错误建议在Silverlight这里, http://forums.silverlight.net/t/ 159414.aspx / 1 ,它适用于Web服务和任何其他.NET解决方案:只需在链接表中添加任何类型的虚拟列。



现在删除原始的.edmx文件,并通过根据实际的数据库生成新的.edmx文件。



然后,intellisense显示链接表,然后你可以插入/创建并执行其他正常操作。



例如,EF 4.0中的Intellisense现在将显示链接表Order_Details,您可以创建或插入(如部分片段,省略try / catch和任何回滚选项):

  using(aDBEntity context = new aDBEntit y())
Order_Details newOrdDetails = new Order_Details();
newOrdDetails.OrderID = // some number here
newOrdDetails.ProductID = // some number here

context.AddToOrder_Details(newOrdDetails);
context.SaveChanges();

问题:是否缺少显示多对多链接表的错误或功能EF 4.0?

解决方案

个人而言,如果您没有任何额外的列,钥匙在里面我从来没有需要访问用于定义M2M关系的链接表。通过获取 Foo Foo Bar 之间的关系, c>(或 Bar )首先使用 Foo.Bars.Add(sampleBar)



我想你回答了你的问题。如果你认为OOP,这是一个功能。如果你想访问链接表(并且你认为你正在做正确的方法),这是缺乏功能。


I have a comment and a question. Entity Framework 4.0 does not show the linking table in a many-to-many relationship between two tables, such as shown in Northwind for "Order_Details", a linking table between Orders and Products, if only two columns, both primary keys, are used in the linking table, as is often the case. So in Northwind if you use as primary keys both OrderID and ProductID in the Order_Details linking table between Orders and Products, for the many-to-many relationship, the linking table will not show up if only these two columns (primary keys) are present in Order_Details.

Consequently, you cannot Insert or Create in a many-to-many relationship linking table, because Entity Framework 4.0 does not show the linking table Order_Details nor does Intellisense show this linking table of the many to many relationship. How is one to then do an insert or update in the linking table if EF 4 does not show the hidden linking table? Arguments such as 'you must now start thinking in OOP' do not impress me. SQL has a certain structure and OOP is just an interface for it so we can use LINQ-to-entities rather than the clumsier SQL queries, IMO.

The trick to get around this bug was suggested for Silverlight here, http://forums.silverlight.net/t/159414.aspx/1 , and it works for web services and any other .NET solution: simply add, in your linking table, a dummy column of any type.

Now delete your original .edmx file, and rebuild a new one by generating it against the actual database.

Then intellisense shows the linking table, and then you can Insert / Create and do other normal oprations.

For example, Intellisense in EF 4.0 will now show the linking table Order_Details, and you can create or insert such as (partial fragment,omitting try/catch and any rollback options):

           using (aDBEntity context = new aDBEntity())
               Order_Details newOrdDetails = new Order_Details();
        newOrdDetails.OrderID = //some number here
        newOrdDetails.ProductID = //some number here

               context.AddToOrder_Details(newOrdDetails);
               context.SaveChanges();

Question: is this lack of showing a many-to-many linking table a bug or a feature of EF 4.0?

解决方案

Personally, I think the linking table is not needed if you don't have any additional column other than two keys in it. I have never needed to access to the link table which is just used to define M2M relationship. I feel relaxed with adding relation between Foo and Bar by getting Foo (or Bar) first and use Foo.Bars.Add(sampleBar).

I think you answered your question. If you think OOP, this is a feature. If you want to have access to the link table (and you think you're doing it the right way), this is a lack of feature.

这篇关于在EF 4.0中缺少多对多链接表的错误或功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆