Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting error use in BulkInsertAsync and BulkUpdateAsync methods #387

Closed
dev07git opened this issue Sep 14, 2020 · 1 comment
Closed

Getting error use in BulkInsertAsync and BulkUpdateAsync methods #387

dev07git opened this issue Sep 14, 2020 · 1 comment
Labels

Comments

@dev07git
Copy link

dev07git commented Sep 14, 2020

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.

@borisdj
Copy link
Owner

borisdj commented Sep 14, 2020

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:

using (var transaction = await context.Database.BeginTransactionAsync())
{
  await context.BulkInsertAsync(entitiesList);
  await context.BulkInsertAsync(subEntitiesList);
  transaction.Commit();
}

If having problems with Deadlock there is useful info in issue/46.

@borisdj borisdj closed this as completed Sep 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants