True merge statement in Entity Framework

c# entity-framework-6

Accepted Answer

It's a strange one, but you could choose records from the "source" entities based on whether or not their IDs were present in the set you already had, and then delete those records 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, bulk deletion is not available.

2
8/28/2015 6:50:49 PM

Popular Answer

I eventually discovered one that does a great work. http://www.zzzprojects.com/entity-framework/library/bulk-merge after sifting through a number of third-party solutions for this.

It was lightning quick and only took one line of code, a nuget install, and a using.

context.BulkMerge(list);

(Bulk update was helpful to me as well.)

context.BulkUpdate(list);

A list of entities is passed into the functions.

My complete context is as follows:

public void Merge(MyDataBaseEntity[] array)
{
     using (var ctx = new Entities())
     {
         var list = array.ToList();
         ctx.BulkMerge(list);
     }
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow