There is no argument given that corresponds to the required formal parameter 'context of GenericRepository.GenericRepository(dbContext)

entity-framework model-view-controller repository-pattern

Question

I am getting this error message which when trying to inherit from my GenericRepository. The error says I need to also provide a context but I am not sure how?

//IncidentRepository 
public class IncidentRepository : GenericRepository<Incident>

//Generic Repository (to inherit from)
public class GenericRepository<TEntity> where TEntity : class
{
internal db_SLee_FYPContext context;
internal DbSet<TEntity> dbSet;

public GenericRepository(db_SLee_FYPContext context)
{
    this.context = context;
    this.dbSet = context.Set<TEntity>();
}

EDIT:

Just to check I've grasped this?

  public class IncidentRepository: GenericRepository<Incident>
  {

    public IncidentRepository(db_SLee_FYPContext context)
    {
        this.context = context;
    }

    //Then in my genric repository
    public GenericRepository()
    {

    }
1
18
11/13/2015 8:58:22 PM

Accepted Answer

The error tells you that you don't call an appropriate base constructor. The constructor in the derived class ...

public IncidentRepository(db_SLee_FYPContext context)
{
    this.context = context;
}

... is in fact doing this:

public IncidentRepository(db_SLee_FYPContext context)
    : base()
{
    this.context = context;
}

But there is no parameterless base constructor.

You should fix this by calling the matching base constructor:

public IncidentRepository(db_SLee_FYPContext context)
    : base(context)
{ }

In C# 6 you get this message if there is only one constructor in the base type, so it gives you the best possible hint which argument in the base constructor is missing. In C# 5 the message would simply be

GenericRepository does not contain a constructor that takes 0 arguments

32
11/15/2015 9:25:46 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow