Does not contain a constructor that takes 0 arguments exception

邮差的信 提交于 2021-01-28 14:15:24

问题


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

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

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

and Product and Entity classes:

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

回答1:


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

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

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



来源:https://stackoverflow.com/questions/24593910/does-not-contain-a-constructor-that-takes-0-arguments-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!