【问题标题】:Does not contain a constructor that takes 0 arguments exception不包含采用 0 参数异常的构造函数
【发布时间】:2023-03-20 02:14:01
【问题描述】:

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

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

这是我的 ProductController 类:

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 类:

 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 接口:

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

【问题讨论】:

  • 你需要将EFDbContext的实例传递给EFProductRepository的构造函数。

标签: c# asp.net-mvc repository


【解决方案1】:

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

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

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

new EFProductRepository<Product>()

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

【讨论】:

  • 您能否展示一下最终代码的样子?
  • @user3427017:好吧,无参数构造函数看起来像这样:public EFProductRepository(){} 但看起来那个类应该有一个无参数构造函数,因为其余的该类假定_context 有一个值。或许无参构造函数可以手动实例化_context
猜你喜欢
  • 2013-02-16
  • 2013-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 2013-02-16
  • 1970-01-01
相关资源
最近更新 更多