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 ):
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)
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?
c # entity-framework-5
Arno 2501
source share