EF 4.0 中缺少多对多链接表是错误还是功能?

发布于 2024-12-15 05:46:19 字数 1287 浏览 0 评论 0原文

我有一个评论和一个问题。 Entity Framework 4.0 不会显示两个表之间多对多关系中的链接表,例如 Northwind 中显示的“Order_Details”(订单和产品之间的链接表),如果仅使用两列(均为主键)在链接表中,这是常见的情况。因此,在 Northwind 中,如果您在 Orders 和 Products 之间的 Order_Details 链接表中使用 OrderID 和 ProductID 作为主键,对于多对多关系,如果仅存在这两列(主键),则链接表将不会显示在订单_详细信息中。

因此,您无法在多对多关系链接表中插入或创建,因为 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 文件,并通过根据实际数据库生成该文件来重建一个新文件。

然后智能感知显示链接表,然后您可以插入/创建并执行其他正常操作。

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

           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();

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空宴 2024-12-22 05:46:19

就我个人而言,我认为如果除了两个键之外没有任何其他列,则不需要链接表。我从来不需要访问仅用于定义 M2M 关系的链接表。通过首先获取 Foo (或 Bar)并使用 FooBar 之间添加关系,我感到很轻松>Foo.Bars.Add(sampleBar)

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文