I have a project called Persistance
where I have installed Nuget Package called EFCore.BulkExtensions for bulk insert.
I have another project called Application
which defines the Interface representing the DBset
as below :
Application Project:
public interface IDatabaseService
{
public DbSet<Employee> Employee {get;set;}
public DbSet<Department> Department {get;set;}
public void Save();
public void Insert<T>(IEnumerable<T> lists);
public int ExecuteSP(string procedureName,params object[] parameters);
}
Persistance project :
using EFCore.BulkExtensions;
public class MyDatabaseContext : DbContext,IDatabaseService
{
private readonly IConfiguration _configuration;
public MyDatabaseContext(IConfiguration configuration)
{
_configuration = configuration;
Database.EnsureCreated();
}
public DbSet<Employee> Employee {get;set;}
public DbSet<Department> Department {get;set;}
public void Save()
{
this.SaveChanges();
}
public void Insert<T>(IEnumerable<T> lists)
{
this.BulkInsert(lists);/error here
}
}
Error: type arguments for method DbContextBulkExtensions.BulkInsert(DbContext, IList) cannot be inferrred from the usage.
Can someone please help?
CodePudding user response:
- The source-code of the
EFCore.BulkExtensions.DbContextBulkExtensions
is available on GitHub.- The
BulkInsert<T>
methods require entities to be passed in viaIList<T>
collections]1, and not asIEnumerable<T>
. - Additionally, they also constrain
T
towhere T : class
, so add that constraint too.
- The
So change this:
public void Insert<T>( IEnumerable<T> lists )
{
this.BulkInsert(lists);/error here
}
to this:
public void Insert<T>( IList<T> entities )
where T : class
{
this.BulkInsert( entities: entities );
}