Reputation: 115
I am using EntityFramework.Utilities NuGet Package to Perform Bulk Insert and Bulk Update. Bulk Insert is working fine for me. But when I am trying to bulk update, I getting following error:
"An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code
Additional information: Incorrect syntax near the keyword 'FROM'."
Following is my code
public virtual void BulkInsert(IEnumerable<TEntity> entities)
{
if (entities == null)
throw new ArgumentNullException(nameof(entities));
EFBatchOperation.For(_Context, _Entities).InsertAll(entities);
}
public virtual void BulkUpdate(IEnumerable<TEntity> entities)
{
if (entities == null)
throw new ArgumentNullException(nameof(entities));
EFBatchOperation.For(_Context, _Entities).UpdateAll(entities,x => x.ColumnsToUpdate());
}
I building a list and sending it for bulk insert
List<SellerGroupAssignment> sellerGroupAssignmentList = new List<SellerGroupAssignment>();
//check all deassignList
foreach (
var modifiedGroup in
sellerList.Where(
current =>
currentSellerGroups.Any(
change => change.SellerId == current.SellerId && !change.IsDeleted)).ToList())
{
var updateGroup = Mapper.Map<SellerGroupAssignment>(modifiedGroup);
updateGroup.Created =
currentSellerGroups.Where(seller => seller.SellerId == updateGroup.SellerId)
.Select(x => x.Created)
.SingleOrDefault();
updateGroup.CreatedByUserId =
currentSellerGroups.Where(seller => seller.SellerId == updateGroup.SellerId)
.Select(x => x.CreatedByUserId)
.SingleOrDefault();
updateGroup.IsDeleted = true;
updateGroup.SellerGroupId = updateGroup.SellerGroupId;
updateGroup.SellerId = updateGroup.SellerId;
updateGroup.Modify();
sellerGroupAssignmentList.Add(updateGroup);
}
sellerGroupAssignmentRepository.BulkUpdate(sellerGroupAssignmentList);
In the Nuget documnetation it is said that, UpdateAll is generic method. We can specify the columns to update if required.
I got stuck with it. Can you please help me.
EDIT
Adding Queries from SQL Profiler
CREATE TABLE dbo.[temp_SellerGroupAssignment_636204526825441555]([SellerId] int, [SellerGroupId] int, PRIMARY KEY ([SellerId], [SellerGroupId]))
go
select @@trancount; SET FMTONLY ON select * from [dbo].[temp_SellerGroupAssignment_636204526825441555] SET FMTONLY OFF exec ..sp_tablecollations_100 N'[dbo].[temp_SellerGroupAssignment_636204526825441555]'
go
insert bulk [dbo].[temp_SellerGroupAssignment_636204526825441555] ([SellerId] Int, [SellerGroupId] Int)
go
UPDATE [SellerGroupAssignment]
SET
FROM
[SellerGroupAssignment] ORIG
INNER JOIN
[temp_SellerGroupAssignment_636204526825441555] TEMP
ON
ORIG.[SellerId] = TEMP.[SellerId] and ORIG.[SellerGroupId] = TEMP.[SellerGroupId]
go
Upvotes: 0
Views: 1568
Reputation: 11
You need to specify the columns you want updated in the call to ColumnsToUpdate()
:
EFBatchOperation.For(_Context, _Entities).UpdateAll(entities,x => x.ColumnsToUpdate());
Currently this is empty, thus no column is specified in the SET part of the SQL query:
SET
FROM
[SellerGroupAssignment] ORIG
You can change this to:
EFBatchOperation.For(_Context, _Entities).UpdateAll(entities,x => x.ColumnsToUpdate(c => c.Created, c => c.CreatedByUserId, c => c.IsDeleted, c => c.SellerGroupId, c => c.SellerId));
Upvotes: 1