You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using EFCore.BulkExtensions[3.1.6] in .Net Core 3.1 Web API and facing two issues.
Issue 1. Getting error if use BulkInsertAsync and BulkUpdateAsync methods
I've two table Models and SubModel and I'm inserting records in these tables using following code:
await vehicleModelDataRepository.BulkInsertAsync(domainModels);
foreach (var domainModel in domainModels.Where(x => x.VehicleSubModels.Count > 0))
{
domainSubModelsInitial.AddRange(domainModel.VehicleSubModels);
}
if(domainSubModelsInitial.Count>0)
await vehicleSubModelDataRepository.BulkInsertAsync(domainSubModelsInitial);
// Data Repository method defination
public async Task<IList<TEntity>> BulkInsertAsync(IList<TEntity> entities, EFCore.BulkExtensions.BulkConfig bulkConfig = null)
{
if (entities == null)
throw new ArgumentException("entity is null");
return await Task.Run(() =>
{
Context.BulkInsertAsync(entities,bulkConfig);
Context.SaveChanges();
return entities;
});
}
During the final commit in Unit Of Work I'm getting the below error:
Microsoft.Data.SqlClient.SqlException: 'The request failed to run because the batch is aborted, this can be caused by abort signal sent from client, or another request is running in the same session, which makes the session busy.'
For BulkUpdateAsync also I'm getting the same error. I'm using the following code for bulk update:
EFCore.BulkExtensions.BulkConfig bulkConfig = new EFCore.BulkExtensions.BulkConfig()
{
PropertiesToInclude = new List<string>() { nameof(VehicleSubModel.SubModel), nameof(VehicleSubModel.Description), nameof(VehicleSubModel.Trim), nameof(VehicleSubModel.ImageUrls) }
};
await vehicleSubModelDataRepository.BulkUpdateAsync(domainUpdatedSubModels,bulkConfig);
// Data Repository method defination
public async Task<IList<TEntity>> BulkUpdateAsync(IList<TEntity> entities, EFCore.BulkExtensions.BulkConfig bulkConfig=null)
{
if (entities == null)
throw new ArgumentException("entity is null");
int i = 0;
return await Task.Run(() =>
{
if(bulkConfig!=null)
Context.BulkUpdateAsync(entities,bulkConfig);
else
Context.BulkUpdateAsync(entities);
i = Context.SaveChanges();
return entities;
});
}
NOTE: If I use BulkUpdate and BulkInsert methods then its working fine for me. Why I'm not able to use the Asyn versions, it seems some deadlock occurs in Asyn methods, but not sure about it.
Issue 2. Sometimes I faced this error, but it is not reproducible.
Microsoft.Data.SqlClient.SqlException: 'Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.'
Please put your valuable comments, so I can understand and learn more.
Thanks much.
The text was updated successfully, but these errors were encountered:
Async methods require keyword await in front of it. await Context.BulkInsertAsync(entities, bulkConfig);
and await Context.BulkUpdateAsync(entities, bulkConfig);
Secondly when using async EF methods SaveChanges has also async version so that should be used: await Context.SaveChangesAsync();
However BulkOps themself do not require SaveChanges as explained in the ReadMe:
When used directly each of these operations are separate transactions and are automatically committed.
And if we need multiple operations in single procedure then explicit transaction should be used, for example:
I'm using EFCore.BulkExtensions[3.1.6] in .Net Core 3.1 Web API and facing two issues.
Issue 1. Getting error if use BulkInsertAsync and BulkUpdateAsync methods
I've two table Models and SubModel and I'm inserting records in these tables using following code:
During the final commit in Unit Of Work I'm getting the below error:
Microsoft.Data.SqlClient.SqlException: 'The request failed to run because the batch is aborted, this can be caused by abort signal sent from client, or another request is running in the same session, which makes the session busy.'
For BulkUpdateAsync also I'm getting the same error. I'm using the following code for bulk update:
NOTE: If I use BulkUpdate and BulkInsert methods then its working fine for me. Why I'm not able to use the Asyn versions, it seems some deadlock occurs in Asyn methods, but not sure about it.
Issue 2. Sometimes I faced this error, but it is not reproducible.
Microsoft.Data.SqlClient.SqlException: 'Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.'
Please put your valuable comments, so I can understand and learn more.
Thanks much.
The text was updated successfully, but these errors were encountered: