Evgeny Ekim
Evgeny Ekim

Reputation: 351

Entity framework Insert doesn't work. Non typical error

I tried to insert row to my entity in Entity Framework. It works good for first insert operation, but it doesn't work for second insert operation. I use Framework 4, C# and FireBird database. This is my code part

rec = new T_MEDIAPLAN_REC();
//
//... initialization for T_MEDIAPLAN_REC
//
BossTVEntities.AddToT_MEDIAPLAN_REC(rec);
BossTVEntities.SaveChanges();

It give me this exception after second SaveChanges() calling:

"The changes to the database were committed successfully, but an error occurred 
while updating the object context. The ObjectContext might be in an inconsistent 
state. Inner exception message: AcceptChanges cannot continue because the 
object's key values conflict with another object in the ObjectStateManager. 
Make sure that the key values are unique before calling AcceptChanges."

I tryed to see DataBase. There are two inserted records in target entity with different Id. So, I don't know what the reason of such behavior.

I use Visual Studio 2010 SP1 Professional, Firebird 2.5 with .NET provider, C#, .NET Framework 4.0

It doesn't work for every entity in database.

Upvotes: 1

Views: 1530

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

This error usually happens when the primary key is generated in the database, but the EF is not properly configured read the value back from the database after the insert. As the result, both inserted items end up with an empty key in the EF context, triggering the error.

Since the inserted data has different keys in the database, your DB schema appears to be correct. You need to check your EDM to see that StoreGeneratedPattern on the PK column is set to Identity or whatever it should be in firebird.

Upvotes: 6

Related Questions