实体框架不会在具有两级关系的新实体上保存更改

发布于 2024-08-02 12:52:31 字数 1370 浏览 6 评论 0原文

我正在使用 ADO.NET 实体框架构建 ASP.NET MVC 站点。 我有一个实体模型,其中包含这些实体,并通过外键关联:

Report(ID、Date、Heading、Report_Type_ID 等)

  • SubReport(ID、ReportText 等) - 与 Report 的一对一关系。
    • ReportSource(ID, Name, Description) - 与 Sub_Report 的一对多关系。
      • ReportSourceType(ID, Name, Description) - 与 ReportSource 的一对多关系。
      • 联系人(ID、姓名、地址等)- 与 Report_Source 的一对一关系。

每种类型的子报表都有一个 Create.aspx 页面。 post 事件方法返回一个新的 Sub_Report 实体。

之前,在我的 post 方法中,我遵循以下过程:

  1. 从页面的字段设置新报表实体的属性。
  2. 从页面字段设置子报表实体的特定属性。
  3. 将 SubReport 实体的 Report 设置为在 1 中创建的新 Report 实体。
  4. 根据页面提供的 ID,查找 ReportSource 并将 Sub_Report 实体的 ReportSource 设置为找到的实体。
  5. 保存更改。

这个工作流程在几周内非常成功。然后上周发生了一些变化,它不再起作用了。现在,我得到了以下异常,而不是保存操作:

UpdateException: "Entities in 'DIR2_5Entities.ReportSourceSet' 
participate in the 'FK_ReportSources_ReportSourceTypes' relationship. 
0 related 'ReportSourceTypes' were found. 1 'Report_Source_Types' is expected."

调试可视化工具显示以下内容:

  • 子报表的 ReportSource 已设置并加载,并且其所有属性均正确。
  • Report_Source 附加了一个有效的 ReportSourceType 实体。

在 SQL Profiler 中,准备好的 SQL 语句看起来没问题。有人能指出我缺少什么明显的东西吗?

TIA

注释: 在这种情况下,报表和子报表始终是新实体。 报告实体包含许多类型的报告共有的属性,并用于一般查询。子报告是具有因类型而异的额外参数的特定报告。实际上,每种类型的 SubReport 都有不同的实体集,但这个问题适用于所有类型,因此我使用 SubReport 作为简化示例。

I'm building an ASP.NET MVC site using the ADO.NET Entity Framework.
I have an entity model that includes these entities, associated by foreign keys:

Report(ID, Date, Heading, Report_Type_ID, etc.)

  • SubReport(ID, ReportText, etc.) - one-to-one relationship with Report.
    • ReportSource(ID, Name, Description) - one-to-many relationship with Sub_Report.
      • ReportSourceType(ID, Name, Description) - one-to-many relationship with ReportSource.
      • Contact (ID, Name, Address, etc.) - one-to-one relationship with Report_Source.

There is a Create.aspx page for each type of SubReport. The post event method returns a new Sub_Report entity.

Before, in my post method, I followed this process:

  1. Set the properties for a new Report entity from the page's fields.
  2. Set the SubReport entity's specific properties from the page's fields.
  3. Set the SubReport entity's Report to the new Report entity created in 1.
  4. Given an ID provided by the page, look up the ReportSource and set the Sub_Report entity's ReportSource to the found entity.
  5. SaveChanges.

This workflow succeeded just fine for a couple of weeks. Then last week something changed and it doesn't work any more. Now instead of the save operation, I get this Exception:

UpdateException: "Entities in 'DIR2_5Entities.ReportSourceSet' 
participate in the 'FK_ReportSources_ReportSourceTypes' relationship. 
0 related 'ReportSourceTypes' were found. 1 'Report_Source_Types' is expected."

The debug visualizer shows the following:

  • The SubReport's ReportSource is set and loaded, and all of its properties are correct.
  • The Report_Source has a valid ReportSourceType entity attached.

In SQL Profiler the prepared SQL statement looks OK. Can anybody point me to what obvious thing I'm missing?

TIA

Notes:
The Report and SubReport are always new entities in this case.
The Report entity contains properties common to many types of reports and is used for generic queries. SubReports are specific reports with extra parameters varying by type. There is actually a different entity set for each type of SubReport, but this question applies to all of them, so I use SubReport as a simplified example.

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

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

发布评论

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

评论(6

断肠人 2024-08-09 12:52:31

我意识到我迟到了,但我也遇到了类似的问题,我花了大约 3 个小时才找到解决方案。我会发布代码,但它在家里 - 如果有人需要,我可以稍后再做。

以下是一些需要检查的事项:

  • 在 SaveChanges() 调用上设置断点并深入检查对象上下文。您应该看到上下文的添加和更改列表。当我第一次查看时,我发现它试图添加所有相关对象,而不仅仅是指向它们。在您的情况下,上下文可能会尝试添加新的 Report_Source_Type。
  • 与上一点相关,但如果您要检索报告源,请确保通过其实体键从数据库中检索它并正确附加到上下文。如果不是,您的上下文可能认为它是一个新项目,因此不会设置其所需的关系。

根据记忆,我使用 context.GetObjectByKey 方法检索了引用,然后使用 context.Attach 方法将这些对象显式附加到上下文,然后将它们分配给我原来的对象。

I realise I'm late to this, but I had a similar problem and I hacked through it for about 3 hours before I came up with a solution. I'd post code, but it's at home - I can do it later if someone needs it.

Here are some things to check:

  • Set a breakpoint on the SaveChanges() call and examine the object context in depth. You should see a list of additions and changes to the context. When I first looked, I found that it was trying to add all my related objects rather than just point to them. In your case, the context might be trying to add a new Report_Source_Type.
  • Related to the previous point, but if you're retrieving the report source, make sure it is being retrieved from the database by its entity key and properly attached to the context. If not, your context might believe it to be a new item and therefore its required relationships won't be set.

From memory, I retrieved my references using the context.GetObjectByKey method, and then explicitly attached those objects to the context using the context.Attach method before assigning them to the properties of my original object.

如梦 2024-08-09 12:52:31

我收到此错误是因为该表没有主键,它有 FK 引用,但没有 PK。

添加 PK 并更新模型后一切正常。

I got this error because the table didn't have a primary key, it had a FK reference, but no PK.

After adding a PK and updating the model all is well.

与他有关 2024-08-09 12:52:31

检查您的 ReportSource 是否加载了 NoTracking 选项或其 EntityState == 'Detached'。如果是这样,那就是你的问题,它必须在上下文中加载。

Check if your ReportSource was loaded with the NoTracking option or if its EntityState == 'Detached'. If so, that is your problem, it must be loaded in the context.

寻找我们的幸福 2024-08-09 12:52:31

如果您的数据库表彼此之间存在 1 - 1 关系,则很可能会发生这种情况。在您的示例中,reportsourceset 需要一个reportsorttypes 及其引用的任何id。当我的关系将相对表中的两个主键链接在一起时,我遇到了这个问题。

This tends to happen if your database tables have a 1 - 1 relationship with each other. In your example reportsourceset expects a reportsorttypes with whatever id it is referencing. I have run into this problem when my relationship is linking two primary keys from opposite tables together.

蓝眼泪 2024-08-09 12:52:31

由于在“已添加”状态下“在幕后”创建了新对象实例,我遇到了同样的错误。这并不明显。

I've got the same error because of new object instance which created "behind the scene" in "Added" state. This was not obvious.

一刻暧昧 2024-08-09 12:52:31

当我将新实体添加到上下文但忘记将新实体添加到对象图中其父级集合时,出现此错误。

例如:

Pet pet = new Pet();
context.Pets.Add(pet);
// forgot this: petOwner.Pets.Add(pet);

I got this error when I added the new entity to the context but forgot to add the new entity to its parent's collection in the object graph.

For example:

Pet pet = new Pet();
context.Pets.Add(pet);
// forgot this: petOwner.Pets.Add(pet);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文