EntityFramework-Extensions icon indicating copy to clipboard operation
EntityFramework-Extensions copied to clipboard

BulkInsert with StreamSize throws IEntityChangeTracker

Open Bouke opened this issue 5 years ago • 7 comments

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)

Bouke avatar Nov 26 '20 10:11 Bouke

Hello @Bouke ,

Thank you for reporting, we will look at it.

Best Regards,

Jon

JonathanMagnan avatar Nov 26 '20 16:11 JonathanMagnan

Hello @Bouke ,

If you could provide us a runnable sample project ([email protected]) it will greatly help us as we cannot reproduce your error.

JonathanMagnan avatar Nov 27 '20 04:11 JonathanMagnan

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

JonathanMagnan avatar Dec 04 '20 13:12 JonathanMagnan

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

JonathanMagnan avatar Dec 11 '20 16:12 JonathanMagnan

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.

Bouke avatar Dec 12 '20 18:12 Bouke

No problem @Bouke!

Let me know if you need anything!

Best regards,

Jon

JonathanMagnan avatar Dec 12 '20 21:12 JonathanMagnan

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

JonathanMagnan avatar Dec 28 '20 13:12 JonathanMagnan