Home > OS >  How to fix type argument error with generic method inside non generic class?
How to fix type argument error with generic method inside non generic class?

Time:10-11

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:

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 );
}
  • Related