Git Product home page Git Product logo

Comments (3)

borisdj avatar borisdj commented on July 17, 2024

It's not clear from your example how do you want to use Transaction, and what are you trying to achieve.
You have more info in README:

When used directly each of BulkOps are separate transactions and are automatically committed.
If we need multiple operations in single procedure then explicit transaction should be used, example:

using (var transaction = context.Database.BeginTransaction())
{
    context.BulkInsert(entitiesList);
    context.BulkInsert(subEntitiesList);
    transaction.Commit();
}

Your example does not have explicit transaction, and as such I have tested it, it works successfully.
I have also made test inside explicit transaction, in which case UseInternalTransaction has to be removed (Must not specify SqlBulkCopyOption.UseInternalTransaction and pass an external Transaction at same time.)
This also runs successfully:

[Fact]
private async Task RunCustomTestAsync()
{
    var entities = new List<Item>();
    for (int i = 1; i < EntitiesNumber; i++)
    {
        entities.Add(new Item
        {
            Name = "name " + i,
            Description = "info " + Guid.NewGuid().ToString().Substring(0, 3),
            Quantity = i % 10,
            Price = i / (i % 5 + 1),
            TimeUpdated = DateTime.Now,
        });
    }
    using (var context = new TestContext(ContextUtil.GetOptions()))
    {
        using (var transaction = context.Database.BeginTransaction())
        {
            await context.BulkInsertAsync(entities, new BulkConfig()
            {
                SetOutputIdentity = true,
                WithHoldlock = false
            });

            int counter = 1;
            foreach (var entity in entities)
            {
                if (counter % 2 == 0)
                {
                    entity.Name = "name Update " + counter;
                    entity.TimeUpdated = DateTime.Now;
                }
                counter++;
            }

            await context.BulkUpdateAsync(entities, new BulkConfig
            {
                SetOutputIdentity = true,
                WithHoldlock = false
            });

            transaction.Commit();
        }
    }
}

from efcore.bulkextensions.

LastImmort avatar LastImmort commented on July 17, 2024

Yeah, next time i need to write more clear example, so i'll try to explain on your test, if it's still make no sense tomorrow i'll try to make demo app with steps to reproduce. Currently i found that remove entities after bulk update from ef change tracker with setting their state to dettached, works for me.

And for my second question about transaction, more correct case is after open transaction with isolation level read commited, make any bulk insert operation and watch in profiler, sql operation successfully completed and i can access data from other client application(in other transactions) while insert in progress, but if i place debug point after bulk insert in c# code, i can't refresh tables in mssql managment studio from db, cause lock timeout, could it be telated to temp tables or with sql bulk copy?

[Fact]
private async Task RunCustomTestAsync() {
    var entities = new List<Item>();
    for (int i = 1; i < EntitiesNumber; i++) {
        entities.Add(new Item {
            Name = "name " + i,
            Description = "info " + Guid.NewGuid().ToString().Substring(0, 3),
            Quantity = i % 10,
            Price = i / (i % 5 + 1),
            TimeUpdated = DateTime.Now,
        });
    }
    using (var context = new TestContext(ContextUtil.GetOptions())) {
        //transaction izolation level in my case was set to ReadCommited
        //var transaction = context.Database.BeginTransaction(IsolationLevel.ReadCommited)
        using (var transaction = context.Database.BeginTransaction()) {
            await context.BulkInsertAsync(entities, new BulkConfig() {
                SetOutputIdentity = true,
                WithHoldlock = false
            });

            int counter = 1;
            foreach (var entity in entities) {
                if (counter % 2 == 0) {
                    entity.Name = "name Update " + counter;
                    entity.TimeUpdated = DateTime.Now;
                }
                counter++;
            }

            await context.BulkUpdateAsync(entities, new BulkConfig {
                SetOutputIdentity = true,
                WithHoldlock = false
            });

            //DO some extra operations inside transaction without bulk, like:
            //var otherEntity =  context.entity.FirstOrDefault(x=>x.Id == 1);
            //otherEntity.prop = 1;

            //then making save changes to apply our operations to transaction
            //here if we watch sql profiler, entries which was in bulk update updated second time by ef change tracker
            //context.SaveChanges();

            transaction.Commit();
        }
    }
}

from efcore.bulkextensions.

borisdj avatar borisdj commented on July 17, 2024

BulkOps are done with SqlBulkCopy that is bypassing EF and its ChangeTracker so it's not useful trying to combine those 2 things. Try to avoid that situation if you can.
For this reason it's best first to close transaction that is doing BulkOps and then in second Transaction do the other regular operations.

Regarding IsolationLevel I haven't use it much so not sure what is the best configuration there.
You can test all combination and see which one works the best.

As for the lock problem read this issue for more info:
Getting Deadlock Issues while doing Bulk insert question
www.github.com/borisdj/EFCore.BulkExtensions/issues/46

from efcore.bulkextensions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.