实体框架不会在具有两级关系的新实体上保存更改
我正在使用 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 的一对一关系。
- ReportSource(ID, Name, Description) - 与 Sub_Report 的一对多关系。
每种类型的子报表都有一个 Create.aspx 页面。 post 事件方法返回一个新的 Sub_Report 实体。
之前,在我的 post 方法中,我遵循以下过程:
- 从页面的字段设置新报表实体的属性。
- 从页面字段设置子报表实体的特定属性。
- 将 SubReport 实体的 Report 设置为在 1 中创建的新 Report 实体。
- 根据页面提供的 ID,查找 ReportSource 并将 Sub_Report 实体的 ReportSource 设置为找到的实体。
- 保存更改。
这个工作流程在几周内非常成功。然后上周发生了一些变化,它不再起作用了。现在,我得到了以下异常,而不是保存操作:
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.
- ReportSource(ID, Name, Description) - one-to-many relationship with Sub_Report.
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:
- Set the properties for a new Report entity from the page's fields.
- Set the SubReport entity's specific properties from the page's fields.
- Set the SubReport entity's Report to the new Report entity created in 1.
- Given an ID provided by the page, look up the ReportSource and set the Sub_Report entity's ReportSource to the found entity.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我意识到我迟到了,但我也遇到了类似的问题,我花了大约 3 个小时才找到解决方案。我会发布代码,但它在家里 - 如果有人需要,我可以稍后再做。
以下是一些需要检查的事项:
根据记忆,我使用 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:
From memory, I retrieved my references using the
context.GetObjectByKey
method, and then explicitly attached those objects to the context using thecontext.Attach
method before assigning them to the properties of my original object.我收到此错误是因为该表没有主键,它有 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.
检查您的 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.
如果您的数据库表彼此之间存在 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.
由于在“已添加”状态下“在幕后”创建了新对象实例,我遇到了同样的错误。这并不明显。
I've got the same error because of new object instance which created "behind the scene" in "Added" state. This was not obvious.
当我将新实体添加到上下文但忘记将新实体添加到对象图中其父级集合时,出现此错误。
例如:
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: