BulkInsert with StreamSize throws IEntityChangeTracker
Description
I have a large collection of objects with related objects that I want to store. This should not happen in-memory as the source and target databases are quite large. I'm setting StreamSize=1000 and the first 1000 are inserted without a problem, including the references properties. However for the second batch, the exception is thrown. I've tried both BulkInsert and BulkMerge, both giving this exception.
Exception
System.InvalidOperationException: 'An entity object cannot be referenced by multiple instances of IEntityChangeTracker.'
at System.Data.Entity.Core.Objects.ObjectContext.VerifyContextForAddOrAttach(IEntityWrapper wrappedEntity)
at System.Data.Entity.Core.Objects.ObjectContext.AddSingleObject(EntitySet entitySet, IEntityWrapper wrappedEntity, String argumentName)
at System.Data.Entity.Core.Objects.DataClasses.EntityReference.AddEntityToObjectStateManager(IEntityWrapper wrappedEntity, Boolean doAttach)
at System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.AddGraphToObjectStateManager(IEntityWrapper wrappedEntity, Boolean relationshipAlreadyExists, Boolean addRelationshipAsUnchanged, Boolean doAttach)
at System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.IncludeEntity(IEntityWrapper wrappedEntity, Boolean addRelationshipAsUnchanged, Boolean doAttach)
at System.Data.Entity.Core.Objects.DataClasses.EntityReference`1.Include(Boolean addRelationshipAsUnchanged, Boolean doAttach)
at System.Data.Entity.Core.Objects.DataClasses.RelationshipManager.AddRelatedEntitiesToObjectStateManager(Boolean doAttach)
at System.Data.Entity.Core.Objects.ObjectContext.AddObject(String entitySetName, Object entity)
at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action`1 action, EntityState newState, IEnumerable entities, String methodName)
at System.Data.Entity.Internal.Linq.InternalSet`1.AddRange(IEnumerable entities)
at System.Data.Entity.DbSet`1.AddRange(IEnumerable`1 entities)
at Z.EntityFramework.Extensions.EntityFrameworkManager.[](DbContext , BulkOperation , IEnumerable`1 , Boolean , Boolean& )
at DbContextExtensions.BulkMerge[T](DbContext this, IEnumerable`1 entities, Action`1 bulkOperationFactory)
at DbContextExtensions.[](EntityBulkOperation`1 , DbContext this, IEnumerable`1 , Action`1 , Action`3 )
at DbContextExtensions.BulkMerge[T](DbContext this, IEnumerable`1 entities, Action`1 bulkOperationFactory)
at ...
Fiddle or Project (Optional)
context.TargetSet.BulkMerge(sourceEnumerable, option =>
{
option.IncludeGraph = true;
option.UnsafeMode = true;
option.StreamSize = 1000;
});
//Having a few related identities m-to-n with DB generated identities:
class Target {
[Key]
int Id { get; set; }
virtual RelatedA RelatedA { get; set; }
virtual RelatedB RelatedB { get; set; }
virtual RelatedC RelatedC { get; set; }
}
class RelatedA {
[Key]
int Id { get; set; }
}
class RelatedB {
[Key]
int Id { get; set; }
}
class RelatedC {
[Key]
int Id { get; set; }
}
// While different `Target`s can reference the same related item:
var a = new RelatedA();
var b = new RelatedB();
var c = new RelatedC();
var enumerable = Enumerable.Range(0, 2000).Select(_ => new Target {
RelatedA = a,
RelatedB = b,
RelatedC = c,
});
Further technical details
- EF version: 6.4.4
- EF Extensions version: 4.0.104
- Database Provider: SqlClient (Azure SQL)
Hello @Bouke ,
Thank you for reporting, we will look at it.
Best Regards,
Jon
Hello @Bouke ,
If you could provide us a runnable sample project ([email protected]) it will greatly help us as we cannot reproduce your error.
Hello @Bouke ,
Since our last conversation, we haven't heard from you.
As mentioned in my previous message, would it be possible to provide a runnable project so that we can help you better?
Feel free to send it to our mailbox [email protected]
Looking forward to hearing from you,
Jon
Hello again!
A simple follow up to see if you had the time to find a runnable project?
Let me know if you need further assistance.
Best regards,
Jon
Hi @JonathanMagnan, thank you for your feedback. I'm looking at providing a runnable project, but that might take a while as we're busy finishing up a project. I hope to get back to you asap.
No problem @Bouke!
Let me know if you need anything!
Best regards,
Jon
Hello @Bouke ,
A simple reminder that we are here to assist you!
Don't hesitate to provide a runnable project so that we can help you better!
Enjoy the holidays & stay safe,
Jon