【问题标题】:Unable to inject IRepository<TEntity> due to "no implicit reference conversion"由于“无隐式引用转换”,无法注入 IRepository<TEntity>
【发布时间】:2019-05-25 15:40:48
【问题描述】:

开始并尝试熟悉 .NET Core 的 ASP.NET 样板模板。

一切都很顺利,直到我需要将带有通用 EntityIRepository 注入到类构造函数中。

DepartmentManager 类中,当我尝试创建一个私有只读字段以注入构造函数时,我收到 IntelliSense 错误:

没有从 Department 到的隐式引用转换 Abp.Domain.Entities.IEntity 类型部门不能用 作为泛型类型或方法中的类型参数 TEntity 'IRepository,主键'

我曾尝试研究是否可以通过一些解决方案在网上找到类似的问题,但无济于事。

下面是一个简单的代码来说明挑战。

// Class 
public class Department
{
    public long Id { get; set; }

    public string DepartmentName { get; set; }
}

// IDepartment Manager
public interface IDepartmentManager
{
    Task<Department> CreateDepartment(Department entity);
    Task Update(Department entity);
    Task Delete(long id);
    Task<IEnumerable<Department>> GetAll();
}

// Department Manager    
private readonly IRepository<Department, long> _departmentRepo;
public DepartmentManager(IRepository<Department, long> departmentRepo)
{
    _departmentRepo = departmentRepo
}

对于我做得不对的地方,我们非常感谢任何帮助。

【问题讨论】:

    标签: c# asp.net-core type-conversion aspnetboilerplate conventions


    【解决方案1】:

    使 Department 实现 Entity&lt;long&gt;IEntity&lt;long&gt; 以使用其存储库。

    public class Department : Entity<long>
    {
        public string DepartmentName { get; set; }
    }
    

    参考文献

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-22
      • 2017-05-15
      相关资源
      最近更新 更多