C# DbContext.Find()上发生引用完整性约束冲突(代码优先)
我遇到了一个相当有趣的例外。它发生在昨天的代码上,这些代码在过去的某个时候曾经工作过(我继承了大部分代码库,以前的开发人员不再与我们在一起) 昨天我使用C# DbContext.Find()上发生引用完整性约束冲突(代码优先),c#,entity-framework,dbcontext,invalidoperationexception,C#,Entity Framework,Dbcontext,Invalidoperationexception,我遇到了一个相当有趣的例外。它发生在昨天的代码上,这些代码在过去的某个时候曾经工作过(我继承了大部分代码库,以前的开发人员不再与我们在一起) 昨天我使用.Where()应用了一个解决方法,但由于今天我在另一个控制器中再次遇到了这个问题,我决定最好在它在意外的地方咬我之前,先弄清它的底细 我得到的错误如下: 发生引用完整性约束冲突:主键 不能删除作为引用完整性约束一部分的属性 除非已设置从属对象,否则在从属对象未更改时更改 该协会的主要对象。主要对象必须是 已跟踪且未标记为删除 现在来看奇怪的部分
.Where()
应用了一个解决方法,但由于今天我在另一个控制器中再次遇到了这个问题,我决定最好在它在意外的地方咬我之前,先弄清它的底细
我得到的错误如下:
发生引用完整性约束冲突:主键
不能删除作为引用完整性约束一部分的属性
除非已设置从属对象,否则在从属对象未更改时更改
该协会的主要对象。主要对象必须是
已跟踪且未标记为删除
现在来看奇怪的部分。当我将Find()方法DbContext.DbSet.Find(int)
与新的DbContext一起使用时,我遇到了这个错误
这是确切的代码。它在第5行找到CurResponse对象,没有发生任何事件,但遇到db.CurFundings.Find(post.SelectedSourceCurFundingKey)时失败并抛出上述异常代码>在下面第7行:
0. [HttpPost]
1. public ActionResult MatchingCur2Fundings(int id, MatchingCur2FundingsVM post, string callbackId)
2. {
3. if (ModelState.IsValid)
4. {
5. var cur = _db.CurResponses.Find(id);
6. var c2f = new Cur2FundingDataFlow();
7. c2f.SourceFunding = _db.CurFundings.Find(post.SelectedSourceCurFundingKey);
我将post.SelectedSourceCurFundingKey
替换为一个整数,如下所示:
7. c2f.SourceCurFunding = _db.CurFundings.Find(1);
我仍然得到上面的错误
当我使用quickwatch\u db.CurFundings
时,它返回3个对象,其中一个对象的主键为1
当我快速观看db.CurFundings.Find(post.SelectedSourceCurFundingKey)时代码>我得到了上面的错误,因此它与作业左侧的内容无关
我可以通过将DbContext.DbSet.Find()
替换为DbContext.DbSet.Where(x=>x.id==id).SingleOrDefault()
来获得所需的项,如下所示:
c2f.SourceCurFunding = _db.CurFundings.Where(cf => cf.CurFundingKey == post.SelectedSourceCurFundingKey).SingleOrDefault();
仅当我使用Find()
方法从DbContext检索项时,才会出现此问题
我使用的是EF CodeFirst,当前代码的大部分是几个月前继承的
我不知道这是否有帮助,但下面是将此特定对象添加到我的DbContext的一行:
public DbSet<CuFunding> CurFundings { get; set; }
编辑2
下面是我可以从Find()
的快速监视中得到的信息:
是否可以显示的类结构?当执行Find
时,您是否看到任何SQL被发出?您好,很抱歉回复太晚。我没有收到SO的任何电子邮件通知。我的问题已用您请求的信息更新。问题不是查找
,而是查找
内部调用的检测更改
。(这也是Where…
不创建异常的原因,因为它不在内部调用DetectChanges
。)如果DetectChanges
抛出这样一个错误,则表示以前发生过一些对象状态的更改。因为在您的Post操作中,Find
之前没有任何更改,我猜您的上下文\u db
不是“新鲜”的,以前发生过一些事情,甚至可能是在以前的请求中,并且上下文没有被正确处理。我检查了条目,它们表示:“枚举未产生任何结果”当我快速观看entry对象的Results视图时。您是否可以尝试使用一个新的上下文:不是\u db
,而是在post操作的开头创建一个新的上下文:var newDb=new YourContext()代码>。然后使用newDB
而不是\u db
,查看是否通过了错误行。
namespace MfpSuite.Models.Documents.CUR
{
public class CurConstructionFunding
{
[Key]
public int CurConstructionFundingKey { get; set; }
public int CurPropertyKey { get; set; }
[ForeignKey("CurPropertyKey")]
public virtual CurProperty CurProperty { get; set; }
public string Source { get; set; }
public string Lender { get; set; }
public string ConstructionAmount { get; set; }
public string PermanentAmount { get; set; }
public string PermanentLoanPerUnit { get; set; }
public string TotalConstructionAmount { get; set; }
public string TotalPermanentAmount { get; set; }
public string TotalPermanentLoanPerUnit { get; set; }
//public int CurResponseKeyId { get; set; }
//[ForeignKey("CurResponseKeyId")]
//public virtual CurResponse CurResponse { get; set; }
/// <summary>
/// Indicates which funding this data is imported into. Note there shouldn't be more than one item in this collection.
/// 01/14/13 - todo - if there shouldn't be more than one, this should be a 1|1 mapping here (like we have [mistakenly] done w/ docTypes to Doc)
/// </summary>
public virtual ICollection<Cur2FundingDataFlow> Cur2FundingDataFlow { get; set; }
}
}
_db.CurConstructionFundings = {SELECT
[Extent1].[CurConstructionFundingKey] AS [CurConstructionFundingKey],
[Extent1].[CurPropertyKey] AS [CurPropertyKey],
[Extent1].[Source] AS [Source],
[Extent1].[Lender] AS [Lender],
[Extent1].[ConstructionAmount] AS [ConstructionAmount],
...
_db.CurConstructionFundings
{SELECT
[Extent1].[CurConstructionFundingKey] AS [CurConstructionFundingKey],
[Extent1].[CurPropertyKey] AS [CurPropertyKey],
[Extent1].[Source] AS [Source],
[Extent1].[Lender] AS [Lender],
[Extent1].[ConstructionAmount] AS [ConstructionAmount],
[Extent1].[PermanentAmount] AS [PermanentAmount],
[Extent1].[PermanentLoanPerUnit] AS [PermanentLoanPerUnit],
[Extent1].[TotalConstructionAmount] AS [TotalConstructionAmount],
[Extent1].[TotalPermanentAmount] AS [TotalPermanentAmount],
[Extent1].[TotalPermanentLoanPerUnit] AS [TotalPermanentLoanPerUnit]
FROM [dbo].[CurConstructionFundings] AS [Extent1]}
StackTrace " at System.Data.Objects.ObjectStateManager.DetectConflicts(IList`1 entries)\r\n
at System.Data.Objects.ObjectStateManager.DetectChanges()\r\n
at System.Data.Objects.ObjectContext.DetectChanges()\r\n
at System.Data.Entity.Internal.InternalContext.DetectChanges(Boolean force)\r\n
at System.Data.Entity.Internal.Linq.InternalSet`1.Find(Object[] keyValues)\r\n
at System.Data.Entity.DbSet`1.Find(Object[] keyValues)" string
+ [System.Reflection.RuntimeMethodInfo] {Void DetectConflicts(System.Collections.Generic.IList`1[System.Data.Objects.EntityEntry])} System.Reflection.RuntimeMethodInfo