Entity Framework 4.0 - EF - 在图中很好地映射了多对多关系

发布于 2024-12-14 09:03:56 字数 804 浏览 1 评论 0原文

抱歉,我刚刚建立了我的第一个 EF 项目,所以 EF 术语说得不太好。我习惯于使用 Linq-to-SQL 作为避免 SQL 查询的一种方式,并且它与我使用的一些 WCF Web 服务配合得很好。所有这些都在 Microsoft Visual Studio 2010 中完成。

现在有了这个背景,我使用 Servr 创建了一个数据库,其中表 A 和 B 与表 C 存在多对多关系。由于在数据库理论中这样做是非法的,因此我设置了一个众所周知,链接表。其中两个:一个称为 A_C,另一个称为 B_C,每个都有一个分别来自表 A 和 B 的外键(1 到多)。然后我将表 C 链接到这两个链接表,这些链接表从 C 接收外键。所以现在我想我没问题了。

因此,我使用实体数据模型向导创建一个 ADO.NET 实体数据模型,选择“来自现有数据库”选项,然后单击按钮。令我惊讶、懊恼、震惊和敬畏的是,我只看到了三张桌子:A、B 和 C!没有任何链接表。他们在开玩笑吗?

想一想这个问题。运行 EF 的引擎发现表 A 和 B 与表 C 共享“多对多”关系。在 .edmx 文件的架构图中,A、B 和 C 之间显示星号,与预期完全一致。

但架构中没有显示链接表!什么?我认为不显示链接表是非常合乎逻辑的……但出乎意料。

对于曾经做过多对多表关系的人来说,这是您的经验吗?

顺便说一句,在按下发送键之前,我对 EF 4.0 做了很多研究,我发现它似乎是一项正在进行的工作。很多陷阱,我可能会掉进去。例如,我打算将其部署在 WCF REST 解决方案中,该解决方案使用与 SQL Server 数据库配合使用的 Web 方法。过去我使用过 Linq-to-SQL,现在我将谨慎地尝试使用 Linq-to-Entities。

Sorry I don't speak EF lingo that well, having just set up my first EF project. I'm used to working with Linq-to-SQL as a way of avoiding SQL queries, and it worked well with some WCF web services I used. All this in Microsoft Visual Studio 2010.

Now with that background, I created a database using Servr that has Tables A and B in a many to many relationship with Table C. Since it is illegal to do this in database theory, I set up a linking table, as is well known. Two of them: one called A_C, and the other called B_C, each having a foreign key (1 to many) from tables A and B, respectively. Then I linked table C t both these linking tables, with these linking tables receiving a foreign key from C. So now I am OK I thought.

So I create an ADO.NET entity data model, using the Entity Data Model Wizard, I picked the option "from an existing database", and clicked the buttons. Much to my surprise, and chagrin, and shock and awe, I saw ONLY THREE TABLES: A, B and C! No linking tables whatsoever. Are they kidding me?

Think about this for a moment. The engine that runs the EF figured out that tables A and B share a "many-to-many" relationship with Table C. In the schema diagram of the .edmx file there is show asterisks between A,B and C, exactly as expected.

BUT NO LINKING TABLES ARE SHOWN IN THE SCHEMA! What? It's very logical I suppose not to show the linking tables...but unexpected.

Is this your experience, for anybody who has ever done many-to-many table relationships?

BTW, before I hit the Send key, I did a lot of research on EF 4.0, and I find it is a work in progress it seems. Lots of pitfalls, that I will probably fall into. For example, I intend to deploy this in a WCF REST solution that uses web methods that work with a SQL Server database. In the past I've used Linq-to-SQL and now I am going to try, guardedly, to use Linq-to-Entities.

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

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

发布评论

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

评论(1

负佳期 2024-12-21 09:03:56

这正是应该发生的事情。所有三个表都将正确更新。

That is exactly what is supposed to happen. And all three tables will be updated correctly.

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