实体框架不会将SaveChanges保存在具有两级关系的新实体上 [英] Entity Framework won't SaveChanges on new entity with two-level relationship

查看:120
本文介绍了实体框架不会将SaveChanges保存在具有两级关系的新实体上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



报告(ID,日期,标题,Report_Type_ID等)




  • SubReport(ID,ReportText等) - 与Report的一对一关系。


    • ReportSource(ID,Name,Description) - 与Sub_Report的一对多关系。


      • ReportSourceType(ID,Name,Description) - 与ReportSource的一对多关系。

      • 联系人(ID ,姓名,地址等) - 与Report_Source的一对一关系。





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



之前,在我的post方法中,我遵循这个过程:


  1. 从页面的字段中设置新的Report实体的属性。

  2. 从页面的字段中设置SubReport实体的特定属性。

  3. 将SubReport实体的报告设置为在1中创建的新报表实体。

  4. 给定页面提供的ID,查找ReportSource并将Sub_Report实体的ReportSource设置为发现实体。

  5. SaveChanges。

这个工作流程成功了几个礼拜。那么上个星期有什么改变,它不再工作了。现在,而不是保存操作,我得到这个例外:

 
UpdateException:DIR2_5Entities.ReportSourceSet中的实体
参与'FK_ReportSources_ReportSourceTypes'关系
0相关'ReportSourceTypes'被发现1'Report_Source_Types'是预期的。

调试可视化程序显示以下内容:




  • SubReport的ReportSource被设置并加载,并且所有的属性都是正确的。

  • Report_Source附有一个有效的ReportSourceType实体。



在SQL Profiler中,准备好的SQL语句看起来可以。任何人可以指出我失踪的明显的事情?



TIA



注意:
在这种情况下,报表和子报表总是新的实体。
报表实体包含许多类型的报表通用的属性,用于通用查询。 SubReports是具有不同类型的额外参数的特定报告。实际上每个类型的SubReport都有一个不同的实体集,但这个问题适用于所有这些,所以我使用SubReport作为一个简化例子。

解决方案

如果您的数据库表彼此之间有1 - 1的关系,则会发生这种情况。在你的例子中,reportsourceset需要一个reportorttypes与其引用的任何id。我遇到这个问题,当我的关系将两个主键从对面的表连接在一起。


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.

解决方案

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.

这篇关于实体框架不会将SaveChanges保存在具有两级关系的新实体上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆