//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.Insert(entities); //using the new extension method
return View();
}
}
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 Insert<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 });
}
}
}
using System.Collections.Generic;
using Abp.Domain.Entities;
using Abp.Domain.Repositories;
using Abp.EntityFramework.Repositories;
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);
}
}
}