ef-bulk-operations icon indicating copy to clipboard operation
ef-bulk-operations copied to clipboard

InsertIfDoesntExist

Open CactusPie opened this issue 6 years ago • 0 comments

Would it be possible to implement a method that inserts a new records if it doesn't exist based on a specified key. Essentially something like:

_context.BulkUpdateAll(new BulkUpdateRequest
{
     Entities = new[] {new User {UserName = "someUserName}},
     InsertIfNew = true,
     KeyPropertyNames = new[] {"UserName"},
     UpdatedColumnNames = new[] {"UserName"},
     Transaction = null
});

Except that doesn't work, since all UpdatedColumnNames that also exist in KeyPropertyNames are filtered out. Update isn't even needed here, for every entity in the list we would only need:

 IF NOT EXISTS(SELECT 1 FROM Users x where x.UserName = 'someUserName')
  INSERT INTO Users(UserName) VALUES ('someUserName');

CactusPie avatar Sep 18 '19 14:09 CactusPie