不包含采用 0 个参数异常的构造函数 [英] Does not contain a constructor that takes 0 arguments exception

查看:128
本文介绍了不包含采用 0 个参数异常的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码有问题.我需要帮助.

I am having issue with my code. I need assistance.

我的代码是 (new EFProductRepository()) 的基础,并说它不包含一个采用 0 个参数的构造函数.

My code underlies the (new EFProductRepository()) and says that it does not contain a constructor that takes 0 arguments.

这是我的 ProductController 类:

Here is my ProductController class:

public class ProductController : ApiController
{
    private readonly IRepository<Product> _repository;

    public ProductController()
        : this(new EFProductRepository<Product>())
    { }

    public ProductController(IRepository<Product> repository)
    {
        _repository = repository;
    }

    public IEnumerable<Product> Get()
    {
        return _repository.Get;
    }

EFProductRepository 类:

EFProductRepository class:

 public class EFProductRepository<T>: IRepository<T> where T: Entity
{
    readonly EFDbContext _context;

    public EFProductRepository(EFDbContext context)
    {
        _context = context;
    }

    public IQueryable<T> Get
    {
        get { return _context.Set<T>();}
    }

    public IQueryable<T> GetIncluding(params Expression<Func<T, object>>[] includeProperties)
    {
        IQueryable<T> query = _context.Set<T>();
        foreach (var includeProperty in includeProperties)
        {
            query = query.Include(includeProperty);
        }
        return query;
    }

    public T Find(object[] keyValues)
    {
        return _context.Set<T>().Find(keyValues);
    }

    public void Add(T entity)
    {
        _context.Set<T>().Add(entity);
    }

    public void Update(T entity)
    {
        var entry = _context.Entry(entity);
        if (entry.State == EntityState.Detached)
        {
            _context.Set<T>().Attach(entity);
            entry = _context.Entry(entity);
        }
        entry.State = EntityState.Modified;
    }

    public void AddOrUpdate(T entity)
    {
        //uses DbContextExtensions to check value of primary key
        _context.AddOrUpdate(entity);
    }

    public void Delete(object[] keyValues)
    {
        //uses DbContextExtensions to attach a stub (or the actual entity if loaded)
        var stub = _context.Load<T>(keyValues);
        _context.Set<T>().Remove(stub);
    }
}

IRepository 接口:

IRepository interface:

public interface IRepository<T> where T : Entity
{

    IQueryable<T> Get { get; }
    IQueryable<T> GetIncluding(params Expression<Func<T, object>>[] includeProperties);
    T Find(object[] keyValues);
    void Add(T entity);
    void Update(T entity);
    void AddOrUpdate(T entity);
    void Delete(object[] keyValues);
}

以及产品和实体类:

public class Product : Entity
{
    public int ProductID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
    public int Quantity { get; set; }
    public string Category { get; set; }
}

public class Entity
{
    public int Id { get; set; }
}

推荐答案

正如错误所述,该类没有采用零参数的构造函数.这是它唯一的构造函数:

Just as the error states, that class has no constructor which takes zero arguments. Here is its only constructor:

public EFProductRepository(EFDbContext context)
{
    _context = context;
}

因此,为了创建一个新实例,您需要向它传递一个 EFDbContext 上下文的实例,而您在这里没有这样做:

So in order to create a new instance, you need to pass it an instance of an EFDbContext context, which you're not doing here:

new EFProductRepository<Product>()

要么为构造函数调用提供一个 EFDbContext 的实例,要么向类中添加一个不需要构造函数的构造函数.

Either supply that constructor call with an instance of an EFDbContext, or add a constructor to the class which doesn't require one.

这篇关于不包含采用 0 个参数异常的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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