I have a process that creates an object that is responsible for inserting into header /detail records. If I run the code to insert into the table on its own, every thing runs fine. However, when I call that code as a separate class within a loop I get an exception:
System.Data.Entity.Infrastructure.DbUpdateException was unhandled
Message=The INSERT statement conflicted with the FOREIGN KEY constraint "FK_CAS_ClaimsAdjustment_Header835". The conflict occurred in database "ERA835DB", table "dbo.Header835", column 'TRANSACTIONID'.
The TRANSACTIONID
Column in the Header
table is set as Identity=True
. The child table, CAS_ClaimsAdjustment
has a column TransactionID
that is FK to Header.TransactionID
.
Why does DBContext.SaveChanges()
seem to behave differently depending on how the same code is called?
foreach (var file in Files)
{
Parser parser = new Parser();
parser.HandleFile(file);
}
public class Parser
{
public void HandleFile(string file)
{
using (Model.DbContext dbcontext)
{
foreach (var itemn in file)
{
Claims claim = new Claims();
// ...
dbcontext.Claims.Add(claim);
}
dbcontext.SaveChanges();
}
}
}
I solved my problem. In my console application I was declaring my entities as "static". Of course this did not work. Once I removed the static keyword everything was fine.