Note that there are some explanatory texts on larger screens.

plurals
  1. POReferential Integrity Constraint Violation occurred on DbContext.Find() (Code First)
    primarykey
    data
    text
    <p>I am encountering a rather interesting exception. It occurred yesterday on code that had worked at some point in the past (I inherited most of the code base and the former developer is no longer with us). </p> <p>I applied a workaround using <code>.Where()</code> yesterday, but since I encountered the problem again in another controller today, I decided it would be best to get to the bottom of it before it bites me somewhere unexpected.</p> <p>I am getting the error below:</p> <blockquote> <p>A referential integrity constraint violation occurred: A primary key property that is a part of referential integrity constraint cannot be changed when the dependent object is Unchanged unless it is being set to the association's principal object. The principal object must be tracked and not marked for deletion.</p> </blockquote> <p>Now for the strange part. I am getting this error when I use the Find() method <code>DbContext.DbSet.Find(int)</code> with a new dbContext.</p> <p>Here is the exact code. It finds the CurResponse object on line 5 without incident, but it fails and throws the exception above when it encounters the <code>db.CurFundings.Find(post.SelectedSourceCurFundingKey);</code> on line 7 below:</p> <pre><code>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); </code></pre> <p>I replaced <code>post.SelectedSourceCurFundingKey</code> with an integer like below:</p> <pre><code>7. c2f.SourceCurFunding = _db.CurFundings.Find(1); </code></pre> <p>And I still get the error above. </p> <p>When I quickwatch <code>_db.CurFundings</code>, it returns 3 objects and one of them has a primary key of <em>1</em>.</p> <p>When I quickwatch the <code>db.CurFundings.Find(post.SelectedSourceCurFundingKey);</code> I get the error above, so it doesn't have anything to do with what is on the left side of the assignment. </p> <p>I can get the item I need by replacing the <code>DbContext.DbSet.Find()</code> with <code>DbContext.DbSet.Where(x =&gt; x.id == id).SingleOrDefault()</code> as in the following:</p> <pre><code>c2f.SourceCurFunding = _db.CurFundings.Where(cf =&gt; cf.CurFundingKey == post.SelectedSourceCurFundingKey).SingleOrDefault(); </code></pre> <p>The issue only occurs when I use the <code>Find()</code> method to retrieve items from the DbContext. </p> <p>I am using EF CodeFirst and the majority of the current code was inherited a few months back.</p> <p>I don't know if this will help, but here is the line that adds this specific object to my DbContext:</p> <pre><code>public DbSet&lt;CuFunding&gt; CurFundings { get; set; } </code></pre> <p>This is the only reference to the <code>DbContext.CurFundings</code> property in the application and when I look at the usages of that type, I don't see anything strange going on.</p> <p>I can always just use the <code>.Where()</code> to get what I need, but I'd like to ensure that this is not a sign of deeper problems.</p> <h1>Edit 1</h1> <p>Here is the entire CurFunding class it is actually <code>CurConstructionFunding</code>, but I shortened the name in the previous example:</p> <pre><code>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; } /// &lt;summary&gt; /// 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) /// &lt;/summary&gt; public virtual ICollection&lt;Cur2FundingDataFlow&gt; Cur2FundingDataFlow { get; set; } } } </code></pre> <h1>Edit 2</h1> <p>Here is what I could pull from a quickwatch of the <code>Find()</code>:</p> <pre><code>_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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload