没有给出对应于所需的形式参数“GenericRepository< Incident> .GenericRepository(dbContext)的上下文的参数” [英] There is no argument given that corresponds to the required formal parameter 'context of GenericRepository<Incident>.GenericRepository(dbContext)

查看:120
本文介绍了没有给出对应于所需的形式参数“GenericRepository< Incident> .GenericRepository(dbContext)的上下文的参数”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从我的GenericRepository继承时,我收到这个错误信息。错误说我需要提供一个上下文,但我不知道如何?

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>();
}

编辑:

只是为了检查我已经掌握了这一点?

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()
    {

    }


推荐答案

错误告诉您,您不调用适当的基础构造函数。派生类中的构造函数...

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;
}

...其实这样做:

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)
{ }

在C#6中,在基类型中只有一个构造函数,所以它给你最好的提示,基本构造函数中缺少哪个参数。在C#5中,消息将仅仅是

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不包含一个构造函数,它需要0个参数

GenericRepository does not contain a constructor that takes 0 arguments

这篇关于没有给出对应于所需的形式参数“GenericRepository&lt; Incident&gt; .GenericRepository(dbContext)的上下文的参数”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆