How to use Entity Framework BulkInsert in Asp.net Zero

908 Views Asked by At

I am using ASP.NET Zero to insert large amounts of data from Excel into the database and it is slow. So I thought of using Entity Framework Extensions for BulkInsert but don't really know how to use it.. I get an error

The type arguments for the method DbContextBulkExtensions.BulkInsert can not be inferred from usage

This is my code.. Any help please?

private readonly IRepository<InventoryBalance> _repository;
   
public void Create(InventoryBalance input)
{
    _repository.GetDbContext().BulkInsert(input);
} 
1

There are 1 best solutions below

0
On

The BulkInsert method takes an IEnumerable<T> parameter. Try changing your code to something like this and pass it a collection of inputs instead of just one.

public void Create(IEnumerable<InventoryBalance> inputs)
{
    _repository.GetDbContext().BulkInsert(inputs);
} 

Also note that you will need to set up the context factory somewhere. Try adding this line to your repository's constructor.

EntityFrameworkManager.ContextFactory = context => context;

https://entityframework-extensions.net/context-factory