Comments (3)
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.
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.
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)
- PostgreSQL - The binary import operation was started with x column(s), but y value(s) were provided.
- BulkInsertOrUpdateAsync performance questions HOT 3
- Using EF Core 8 Complex Property results in error "Sequence contains no matching element"
- When using PostgreSQL to batch insert DateTime.Now data, there might be a time offset.
- BulkExtensions determines target DbContext incorrectly when calls to different database servers in parallel HOT 1
- VULN: CVE-2024-32655 in Npgsql dependency HOT 3
- BulkInsertOrUpdate doesn't return Identity when no insert or update happen
- not present in the dictionary.
- can BulkInsert support my custom ValueGenerator for Id column HOT 1
- BulkSaveChangesAsync leaves tracked entities in state of "Added" even after successful save. HOT 2
- Spatial support in SQLite for Point
- BulkRead: System.NotSupportedException: 'Specified method is not supported.'
- BulkInsert produces sytanx error on PostgreSQL (version 6.x) HOT 1
- Bulk operation fails when owned type has enum member on PostgreSQL
- `SharedTypeEntity` don't work with bulk operations
- [MySql/MariaDb] Insert an boolean type has invalid result (Always insert true in a bit column)
- Question : Set value on shadow properties "on save"
- Unable to cast object of type 'System.Int64' to type 'System.Nullable`1[System.Int32]'
- PostgreSQL Table-Per-Hierarchy Inheritance Is Not Working
- Truncate method with custom table name
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from efcore.bulkextensions.