Error adding new object to EF: referential integrity constraint violation

I add a new object that is created through an external class, and I get this error: 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 principal object. The principal object must be tracked and not marked for deletion. 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 principal object. The principal object must be tracked and not marked for deletion. :

Edit : ReconFact object ReconFact not associated with any other objects

Code ( edited ):

 // the context class AccountingModelContext db = new AccountingModelContext(); // query some data List<Reconciliation> recon = db.Reconciliations .Where(r => r.ReconNum == 112293) // scenario 18 .Include(r => r.ReconciliationDetails.Select(rd => rd.JrnlEntryDetail).Select(jd => jd.JrnlEntry).Select(j => j.ARInvoices.Select(i => i.ARInvoiceDetails))) .Include(r => r.ReconciliationDetails.Select(rd => rd.JrnlEntryDetail).Select(jd => jd.JrnlEntry).Select(j => j.ARCredMemoes.Select(c => c.ARCredMemoDetails))) .ToList(); // class that will manager business logic and generate facts with above data ReconFactGenerator generator = new ReconFactGenerator(recon); // call to the method that will return a list of facts List<ReconFact> scenario18 = generator.GenerateFacts(); // ERROR RAISED HERE scenario18.ForEach(f => db.ReconFacts.Add(f)); // never reached db.SaveChanges(); 

It is very simple. I am building a list of ReconFact entities with the ReconFactGenerator class. Identifiers are not specified, as they are new objects.

edited It works fine if I create a new object in the same method as here. Before I request data from the context:

 ReconFact testfact = new ReconFact { ItemCode = "test", ReconNum = 1234, ReconDate = DateTime.Parse("2099-01-01"), ShortName = "L_SZTREDFD", InvoiceAmount = 0, CredMemoAmount = 0, IncomingPayAmount = 0, OutgoingPayAmount = 0, JrnlEntryAmount = 0, OtherAmount = 0 }; db.ReconFacts.Add(testfact); // no problems no error raised db.SaveChanges(); // I can see the new record in my database // the ID was generated successfully // as I've specified identity(1,1) in the database 

Added Edit 2 After requesting data from the context as follows:

  List<Reconciliation> recon = db.Reconciliations .Where(r => r.ReconNum == 112293) // scenario 18 .Include(r => r.ReconciliationDetails.Select(rd => rd.JrnlEntryDetail).Select(jd => jd.JrnlEntry).Select(j => j.ARInvoices.Select(i => i.ARInvoiceDetails))) .Include(r => r.ReconciliationDetails.Select(rd => rd.JrnlEntryDetail).Select(jd => jd.JrnlEntry).Select(j => j.ARCredMemoes.Select(c => c.ARCredMemoDetails))) .ToList(); 

The context is not able to save anymore, returning the above error.

edited Why does an error occur when I want to save after a context request and does not cause an error earlier?

+7
c # entity-framework-5
source share
2 answers

Learn how to get around, although it does not explain why the error occurs:

 // the context class AccountingModelContext db = new AccountingModelContext(); // query some data List<Reconciliation> recon = db.Reconciliations .Where(r => r.ReconNum == 112293) // scenario 18 .Include(r => r.ReconciliationDetails.Select(rd => rd.JrnlEntryDetail).Select(jd => jd.JrnlEntry).Select(j => j.ARInvoices.Select(i => i.ARInvoiceDetails))) .Include(r => r.ReconciliationDetails.Select(rd => rd.JrnlEntryDetail).Select(jd => jd.JrnlEntry).Select(j => j.ARCredMemoes.Select(c => c.ARCredMemoDetails))) .ToList(); // class that will manager business logic and generate facts with above data ReconFactGenerator generator = new ReconFactGenerator(recon); // call to the method that will return a list of facts List<ReconFact> scenario18 = generator.GenerateFacts(); // ERROR RAISED HERE // scenario18.ForEach(f => db.ReconFacts.Add(f)); // INSTEAD OF REUSING THE CONTEXT I CREATE A NEW ONE PROBLEM SOLVED ! using (db = new AccountingModelContext()) { scenario18.ForEach(f => db.ReconFacts.Add(f)); db.SaveChanges(); } db.SaveChanges(); 
+1
source share

Out of interest, you get the same problem when you replace List<T>.ForEach() actual ForEach , so change:

 scenario18.ForEach(f => db.ReconFacts.Add(f)); 

To:

 foreach(var f in scenario18) { db.ReconFacts.Add(f); } db.SaveChanges(); 
0
source share

All Articles