Nuget packages that extend the DbContext in EF6/EFCore with bulk operations.
Read the CodeProject article Bulk operations using Entity Framework if you are interested in some background.
The extension is built for, and requires, Entity Framework 6 or EF Core.
This will make the following methods available on the DbContext.
public class BulkInsertRequest<T>
{
public IList<T> Entities { get; set; }
public SqlTransaction Transaction { get; set; }
public bool UpdateStatistics { get; set; } = false;
public bool Recursive { get; set; } = false;
public bool AllowNotNullSelfReferences { get; set; } = false;
public bool SortUsingClusteredIndex { get; set; } = true;
}
public static BulkOperationResponse BulkInsertAll<T>(
this DbContext ctx,
BulkInsertRequest<T> request)
public class BulkUpdateRequest
{
public IList Entities { get; set; }
public string[] UpdatedPropertyNames { get; set; }
public string[] KeyPropertyNames { get; set; }
public SqlTransaction Transaction { get; set; }
public bool InsertIfNew { get; set; }
}
public static BulkOperationResponse BulkUpdateAll(
this DbContext ctx,
BulkUpdateRequest request)
The select-existing feature provides a way to identify the subset of existing or non-existing items in a local collection where an item is considered as existing if it is equal to an entity saved in the database according to a set of defined key properties. This provides a very efficient way of figuring out which items in your local collection needs to be inserted and which to be updated. The item collection can be of the same type as the EF entity but it does not have to be.
public class BulkSelectRequest<T>
{
public BulkSelectRequest(string[] keyPropertyNames,
IList<T> items = null,
SqlTransaction transaction = null)
{
KeyPropertyMappings = keyPropertyNames.Select(n => new KeyPropertyMapping
{
ItemPropertyName = n,
EntityPropertyName = n
})
.ToArray();
Items = items;
Transaction = transaction;
}
public IList<T> Items { get; set; }
public KeyPropertyMapping[] KeyPropertyMappings { get; set; }
public SqlTransaction Transaction { get; set; }
public BulkSelectRequest()
{
KeyPropertyMappings = new KeyPropertyMapping[0];
Items = new T[0];
}
}
public class KeyPropertyMapping
{
public string ItemPropertyName { get; set; }
public string EntityPropertyName { get; set; }
public static KeyPropertyMapping[] IdentityMappings(string[] names)
{
return names.Select(n => new KeyPropertyMapping
{
ItemPropertyName = n,
EntityPropertyName = n
}).ToArray();
}
}
/// <summary>
/// Given a set of entities we return the subset of these entities
/// that already exist in the database, according to the key selector
/// used.
/// </summary>
/// <typeparam name="T1">The item collection type</typeparam>
/// <typeparam name="T2">The EF entity type</typeparam>
/// <param name="ctx"></param>
/// <param name="request"></param>
public static IList<T1> BulkSelectExisting<T1,T2>(
this DbContext ctx,
BulkSelectRequest<T1> request)
NOT IMPLEMENTED
public class BulkDeleteRequest<T>
{
public SqlCondition[] SqlConditions { get; set; }
public KeyPropertyMapping[] KeyPropertyMappings { get; set; }
public IList<T> Items { get; set; }
public SqlTransaction Transaction { get; set; }
public TimeSpan CommandTimeout { get; set; } = TimeSpan.FromMinutes(1);
public BulkDeleteRequest(
SqlCondition[] sqlConditions,
string[] keyPropertyNames,
IList<T> items = null,
SqlTransaction transaction = null)
{
SqlConditions = sqlConditions;
KeyPropertyMappings = KeyPropertyMapping.IdentityMappings(keyPropertyNames);
Items = items;
Transaction = transaction;
}
public BulkDeleteRequest()
{
KeyPropertyMappings = new KeyPropertyMapping[0];
Items = new T[0];
}
}
/// <summary>
/// The bulk delete request contains a SqlCondition. It has
/// a list of column name/column value pairs and will be used
/// to build an AND where clause. This method will delete any
/// rows in the database that matches this SQL condition unless
/// it also matches one of the supplied entities according to
/// the key selector used.
///
/// !!! IMPORTANT !!!
/// MAKE SURE THAT YOU FULLY UNDERSTAND THIS LOGIC
/// BEFORE USING THE BULK DELETE METHOD SO THAT YOU
/// DO NOT END UP WITH AN EMPTY DATABASE.
///
/// </summary>
/// <typeparam name="T1"></typeparam>
/// <typeparam name="T2"></typeparam>
/// <param name="ctx"></param>
/// <param name="request"></param>
public static void BulkDeleteNotExisting<T1, T2>(
this DbContext ctx,
BulkDeleteRequest<T1> request)
We use SemVer for versioning. For the versions available, see the tags on this repository.
This project is licensed under the Apache License - see the LICENSE.md file for details.