I am having an error in my Controller. The repository can't be injected in the base constructor
public class MyController : ControllerCRUDBase<Entity, TournamentDto>
{
public MyController (IGenericRepository<Entity> repository, IMapper mapper)
: base(repository, mapper)
{
}
}
Where my ControllerCRUDBase is:
public abstract class ControllerCRUDBase<E, D> : ControllerBase
where E : EntityBase
where D : DtoBase
{
protected readonly IGenericRepository<EntityBase> _repository;
protected readonly IMapper _mapper;
public ControllerCRUDBase(IGenericRepository<EntityBase> repository,
IMapper mapper)
{
_repository = repository;
_mapper = mapper;
}
My IGenericRepository is:
public interface IGenericRepository<TEntity> where TEntity : EntityBase
{
}
And the Entity class is:
public class Entity: EntityBase
{
}
The error is this:
cannot convert from 'IGenericRepository<Entity>' to 'IGenericRepository<EntityBase>'
And I can't understand why.
You're defining a constraint on the generic parameter E
for ControllerCRUDBase
, but then directly referencing EntityBase
in the subsequent constructor instead. Try the following:
public abstract class ControllerCRUDBase<E, D> : ControllerBase
where E : EntityBase
where D : DtoBase
{
protected readonly IGenericRepository<E> _repository;
protected readonly IMapper _mapper;
public ControllerCRUDBase(IGenericRepository<E> repository,
IMapper mapper)
{
_repository = repository;
_mapper = mapper;
}
}