从一个对象到同一实体类型的两个对象的多个关联

发布于 2024-11-06 18:03:34 字数 703 浏览 10 评论 0原文

我尝试首先使用代码和流畅的 API 创建一个对象,该对象包含同一个表中的两个不同实体。换句话说,传输对象保存对两个不同坦克对象的引用——一个是源,另一个是目的地。

但是,当我使用以下代码时,我收到一个异常,指出“引用关系将导致不允许的循环引用”。

modelBuilder.Entity<Transfer>()
            .HasRequired<Tank>(t => t.Source)
            .WithMany(t => t.OutboundTransfers);
modelBuilder.Entity<Transfer>()
            .HasRequired<Tank>(t => t.Destination)
            .WithMany(t => t.InboundTransfers);

我最好的猜测是它认为我将两把钥匙都指向同一个坦克?知道我怎样才能做到这一点吗?

编辑:找到答案,从 实体框架代码优先 - 来自同一个表的两个外键

I am attempting to use code first and the fluent API to create an object that holds two different entities from the same table. In other words, a transfer object holds a reference to two different tank objects--one is the source and the other the destination.

However, when I use the following code I get an Exception stating that "The referential relationship will result in a cyclical reference that is not allowed."

modelBuilder.Entity<Transfer>()
            .HasRequired<Tank>(t => t.Source)
            .WithMany(t => t.OutboundTransfers);
modelBuilder.Entity<Transfer>()
            .HasRequired<Tank>(t => t.Destination)
            .WithMany(t => t.InboundTransfers);

My best guess is that it thinks I am pointing both keys to the same Tank? Any idea how I can accomplish this?

EDIT: Found the answer as adding .WillCascadeOnDelete(false) from Entity Framework Code First - two Foreign Keys from same table

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

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

发布评论

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

评论(2

我的鱼塘能养鲲 2024-11-13 18:03:34

正如您所说,您应该能够添加 .WillCascadeOnDelete(false) - https://stackoverflow.com /a/5559300/5416

modelBuilder.Entity<Transfer>()
        .HasRequired<Tank>(t => t.Source)
        .WithMany(t => t.OutboundTransfers)
        .WillCascadeOnDelete(false);
modelBuilder.Entity<Transfer>()
        .HasRequired<Tank>(t => t.Destination)
        .WithMany(t => t.InboundTransfers)
        .WillCascadeOnDelete(false);

我刚刚添加了此答案,以便它不再显示在未答复列表中,因为未答复的答案为零。标记为社区 wiki :)

As you stated, you should be able to add .WillCascadeOnDelete(false) - https://stackoverflow.com/a/5559300/5416

modelBuilder.Entity<Transfer>()
        .HasRequired<Tank>(t => t.Source)
        .WithMany(t => t.OutboundTransfers)
        .WillCascadeOnDelete(false);
modelBuilder.Entity<Transfer>()
        .HasRequired<Tank>(t => t.Destination)
        .WithMany(t => t.InboundTransfers)
        .WillCascadeOnDelete(false);

I just added this answer so that it doesn't show on the unanswered list any more as unanswered with zero answers. Marked as community wiki :)

夜清冷一曲。 2024-11-13 18:03:34

像往常一样,Ladislav(EF 大师)有 EF 问题的答案。 (感谢拉迪斯拉夫)
只是我发现有用的替代方案。
我有并行运行的第二个解决方案。
如果我每个人都有代码优先问题。我首先尝试一下:
逆向工程师对第一个解决方案进行编码。(来自 EF 电动工具)

我有一个测试数据库,我在数据库中手动添加所需的概念。
我仍然发现在数据库中执行此操作更直观,因为我首先是代码新手。
然后使用电动工具
http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b -89f2-846072eff19d

在项目/解决方案上右键单击实体框架,首先对数据库进行逆向工程以进行代码。

查看它生成的代码。你会得到一个如何做某事的例子。

As usual Ladislav (EF Guru) has the answer to EF questions. (thanks Ladislav)
Just an alternate i have found useful.
I have an second solution I run in parallel.
If I every have a code first issue. I first try thi:
The reverse engineer to code first solution.(from EF power tools)

I have a test DB where I add the required concept by hand in a DB.
I still find doing this in the DB more intuitive as im new to code first.
Then using the powertool
http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d

on the Project/Solution right click Entity framework, reverse engineer DB to code first.

Check out the code it generartes. you get an example of how to do somethings.

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