-
-
Save rqx110/38df69360cc8f5f37dee75fa1f94e742 to your computer and use it in GitHub Desktop.
扩展IRepository,支持批量插入
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
//Demo usage | |
public class HomeController : MyControllerBase | |
{ | |
private readonly IRepository<MyEntity> _myEntityRepository; | |
public HomeController(IRepository<MyEntity> myEntityRepository) | |
{ | |
_myEntityRepository = myEntityRepository; | |
} | |
public ActionResult Index() | |
{ | |
var entities = new List<MyEntity> | |
{ | |
new MyEntity(), | |
new MyEntity(), | |
new MyEntity() | |
}; | |
_myEntityRepository.BulkInsert(entities); //using the new extension method | |
return View(); | |
} | |
} |
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
using System; | |
using System.Collections.Generic; | |
using System.Reflection; | |
using Abp.Domain.Entities; | |
using Abp.Domain.Repositories; | |
namespace MyProject | |
{ | |
//Add this class to .Core project | |
public static class MyRepositoryExtensions | |
{ | |
public static void BulkInsert<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository, | |
IEnumerable<TEntity> entities) | |
where TEntity : class, IEntity<TPrimaryKey>, new() | |
{ | |
var type = Type.GetType("MyProject.MyRepositoryHelper, MyProject.EntityFramework"); | |
var bulkInsertMethod = type.GetMethod("BulkInsert", BindingFlags.Static | BindingFlags.Public); | |
var genericMethod = bulkInsertMethod.MakeGenericMethod(typeof(TEntity), typeof(TPrimaryKey)); | |
genericMethod.Invoke(null, new object[] { repository, entities }); | |
} | |
public static void BulkDelete<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository, | |
Expression<Func<TEntity, bool>> predicate) | |
where TEntity : class, IEntity<TPrimaryKey>, new() | |
{ | |
var type = Type.GetType("SimpleTaskSystem.MyRepositoryHelper, SimpleTaskSystem.EntityFramework"); | |
var bulkDeleteMethod = type.GetMethod("BulkDelete", BindingFlags.Static | BindingFlags.Public); | |
var genericMethod = bulkDeleteMethod.MakeGenericMethod(typeof(TEntity), typeof(TPrimaryKey)); | |
genericMethod.Invoke(null, new object[] { repository, predicate }); | |
} | |
public static void BulkUpdate<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository, | |
Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TEntity>> updateExpression) | |
where TEntity : class, IEntity<TPrimaryKey>, new() | |
{ | |
var type = Type.GetType("SimpleTaskSystem.MyRepositoryHelper, SimpleTaskSystem.EntityFramework"); | |
var bulkUpdateMethod = type.GetMethod("BulkUpdate", BindingFlags.Static | BindingFlags.Public); | |
var genericMethod = bulkUpdateMethod.MakeGenericMethod(typeof(TEntity), typeof(TPrimaryKey)); | |
genericMethod.Invoke(null, new object[] {repository, predicate, updateExpression}); | |
} | |
} | |
} |
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
using System.Collections.Generic; | |
using Abp.Domain.Entities; | |
using Abp.Domain.Repositories; | |
using Abp.EntityFramework.Repositories; | |
using EntityFramework.Extensions; | |
namespace MyProject | |
{ | |
//Add this class to .EntityFramework project | |
public static class MyRepositoryHelper | |
{ | |
public static void BulkInsert<TEntity, TPrimaryKey>(IRepository<TEntity, TPrimaryKey> repository, IEnumerable<TEntity> entities) | |
where TEntity : class, IEntity<TPrimaryKey>, new() | |
{ | |
var dbContext = repository.GetDbContext(); | |
dbContext.Set<TEntity>().AddRange(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).Delete(); | |
} | |
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).Update(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/d082866216adbb272069cc68bf7feaec