Error when inserting using Entity Framework

  • Thread starter Thread starter Anders Ljusberg
  • Start date Start date
A

Anders Ljusberg

Hi!

I'm writing an application for transforming quite large amounts of data from
one database to another and I'm using Entity Framework for all data access.

My application runs just fine processing thousands of rows per second, until
it reaches about 700 000 rows, suddenly when trying to save the latest
"batch" I get the following exception "An item with the same key has already
been added".

The stack trace looks like this:

at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value,
Boolean add)
at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
at
System.Data.Mapping.Update.Internal.TableChangeProcessor.ProcessKeys(UpdateCompiler
compiler, List`1 changes, Set`1 keys)
at
System.Data.Mapping.Update.Internal.TableChangeProcessor.CompileCommands(ChangeNode
changeNode, UpdateCompiler compiler)
at
System.Data.Mapping.Update.Internal.UpdateTranslator.<ProduceDynamicCommands>d__0.MoveNext()
at System.Linq.Enumerable.<ConcatIterator>d__71`1.MoveNext()
at
System.Data.Mapping.Update.Internal.UpdateCommandOrderer..ctor(IEnumerable`1
commands, UpdateTranslator translator)
at System.Data.Mapping.Update.Internal.UpdateTranslator.ProduceCommands()
at
System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager
stateManager, IEntityAdapter adapter)
at System.Data.Objects.ObjectContext.SaveChanges(Boolean
acceptChangesDuringSave)


Any clues as to what is going on here? Note that it is not a Primary Key
exception or something similar - the error seems to occur somewhere in the
middle of the SaveChanges method. This means that it is really difficult for
me to debug since I have no idea which of the (thousands of) rows that is
generating the exception..

/Anders Ljusberg
 
Turns out this was in fact a kind of Primary Key violation. I have a
composite key (two integers), and I didn't get any error when adding them to
the collection, but when saving it appears that some internal process finds
the error. Might be some room here for improving the error message?

/Anders
 
Back
Top