Created
November 13, 2017 02:33
-
-
Save rqx110/d082866216adbb272069cc68bf7feaec to your computer and use it in GitHub Desktop.
another abp bulk/batch operation extesion base on http://entityframework-extensions.net
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class EfRepositoryBulkExtensions | |
{ | |
public static void BulkInsert<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository, IEnumerable<TEntity> entities) | |
where TEntity : class, IEntity<TPrimaryKey>, new() | |
{ | |
var dbContext = repository.GetDbContext(); | |
dbContext.BulkInsert(entities); | |
} | |
public static async Task BulkInsertAsync<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository, IEnumerable<TEntity> entities) | |
where TEntity : class, IEntity<TPrimaryKey>, new() | |
{ | |
var dbContext = repository.GetDbContext(); | |
await dbContext.BulkInsertAsync(entities); | |
} | |
public static void BulkDelete<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository, IEnumerable<TEntity> entities) | |
where TEntity : class, IEntity<TPrimaryKey>, new() | |
{ | |
var dbContext = repository.GetDbContext(); | |
dbContext.BulkDelete(entities); | |
} | |
public static void BulkDelete<TEntity, TPrimaryKey>(IRepository<TEntity, TPrimaryKey> repository, Expression<Func<TEntity, bool>> predicate) | |
where TEntity : class, IEntity<TPrimaryKey>, new() | |
{ | |
var dbContext = repository.GetDbContext(); | |
dbContext.Set<TEntity>().Where(predicate).DeleteFromQuery(); | |
} | |
public static async Task BulkDeleteAsync<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository, IEnumerable<TEntity> entities) | |
where TEntity : class, IEntity<TPrimaryKey>, new() | |
{ | |
var dbContext = repository.GetDbContext(); | |
await dbContext.BulkDeleteAsync(entities); | |
} | |
public static async Task BulkDeleteAsync<TEntity, TPrimaryKey>(IRepository<TEntity, TPrimaryKey> repository, Expression<Func<TEntity, bool>> predicate) | |
where TEntity : class, IEntity<TPrimaryKey>, new() | |
{ | |
var dbContext = repository.GetDbContext(); | |
await dbContext.Set<TEntity>().Where(predicate).DeleteFromQueryAsync(); | |
} | |
public static void BulkUpdate<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository, IEnumerable<TEntity> entities) | |
where TEntity : class, IEntity<TPrimaryKey>, new() | |
{ | |
var dbContext = repository.GetDbContext(); | |
dbContext.BulkUpdate(entities); | |
} | |
public static void BulkUpdate<TEntity, TPrimaryKey>(IRepository<TEntity, TPrimaryKey> repository, Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TEntity>> updateExpression) | |
where TEntity : class, IEntity<TPrimaryKey>, new() | |
{ | |
var dbContext = repository.GetDbContext(); | |
dbContext.Set<TEntity>().Where(predicate).UpdateFromQuery(updateExpression); | |
} | |
public static async Task BulkUpdateAsync<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository, IEnumerable<TEntity> entities) | |
where TEntity : class, IEntity<TPrimaryKey>, new() | |
{ | |
var dbContext = repository.GetDbContext(); | |
await dbContext.BulkUpdateAsync(entities); | |
} | |
public static async Task BulkUpdateAsync<TEntity, TPrimaryKey>(IRepository<TEntity, TPrimaryKey> repository, Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TEntity>> updateExpression) | |
where TEntity : class, IEntity<TPrimaryKey>, new() | |
{ | |
var dbContext = repository.GetDbContext(); | |
await dbContext.Set<TEntity>().Where(predicate).UpdateFromQueryAsync(updateExpression); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
also reference to https://gist.github.com/rqx110/38df69360cc8f5f37dee75fa1f94e742