1

Is there a true equivalent of the T-SQL MERGE statement in EF? I know they added AddOrUpdate in a previous version which obviously handles the add or update aspect. It's missing the when not matched from source then delete from the target table.

This particular process is literally just syncing down a third party json structure wholesale. I haven't found anything on a wrapped add / update / delete function in Entity Framework. Thought I would ask on here or for any creative thoughts on wrapping this versus enumerating over the entity, checking for existence, and deleting where the key exists in the DB but not in the source entity.

Thanks.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
PMOrion
  • 169
  • 2
  • 10
  • Possible duplicate of [MERGE in Entity Framework](http://stackoverflow.com/questions/5842125/merge-in-entity-framework) – Jon Schneider Jun 16 '16 at 12:43

3 Answers3

2

It's a bit of an odd one but you could select records based on their ID not being contained in the set you have from the 'source' entities, then delete those from the DbContext.

var sourceIds = source.Select(s=>s.Id);
var notFounds = context.Target.Select(s=>!sourceIds.Contains(s.Id));
foreach (var notFound in notFounds) {
  context.Target.DeleteObject(notFound);
}

Unfortunately there's not bulk delete.

Lazarus
  • 41,906
  • 4
  • 43
  • 54
2

After pouring thru a series of 3rd party solutions for this, I finally found one that does a perfect job. http://www.zzzprojects.com/entity-framework/library/bulk-merge

It's blazing fast and required a nuget install, a using, and one line of code.

context.BulkMerge(list);

(I also found bulk update useful.)

context.BulkUpdate(list);

The functions take in a list of entities.

My full context is something like:

public void Merge(MyDataBaseEntity[] array)
{
     using (var ctx = new Entities())
     {
         var list = array.ToList();
         ctx.BulkMerge(list);
     }
}
SFlagg
  • 379
  • 5
  • 10
  • @Azimuth mmmm... I think it was free in March. I've definitely never paid for it. On their site they say *"PRO Version unlocked for the current month"*. There's also http://entityframework-plus.net/ which I guess is their open source project. That's free and has a lot of similar functions. – SFlagg May 18 '17 at 18:29
  • 2
    if you look on their website [entityframework-plus.net](http://entityframework-plus.net/), there is no `BulkMerge` and `BulkUpdate`. Those exist only in the paid version. – Azimuth May 18 '17 at 18:56
0

Not natively to EF.

There's this (https://gist.github.com/ondravondra/4001192) that creates a sql merge statement. It doesn't include the deletion but it's opensoure and should be easy to add a WHEN NOT MATCHED BY SOURCE THEN DELETE or something.

Disclaimer: I'm not associated with that code at all, and just found it around

Or also, you could just use a stored procedure

Jcl
  • 27,696
  • 5
  • 61
  • 92
  • Modifying this extension method in the link may be a good alternative. Would be nice if the framework supported this natively. – PMOrion Aug 28 '15 at 19:56