小编Moh*_*yan的帖子

在通用接口上使用“工作单元”时,为什么会看到错误?

我有一个名为:的通用类存储库GenericRepository,还有一个名为:的通用接口,IGenericRepository并且Product是数据库中的表之一。

当我通过这种方式将“工作单元”与此通用存储库一起使用时:

public class UnitOfWork: IDisposable
{
    GroceryStore_DBEntities db = new GroceryStore_DBEntities();
    private IGenericRepository<Product> _genericRepository;
    public IGenericRepository<Product> GenericRepository
    {
        get
        {
            if (_genericRepository == null)
            {
                _genericRepository = new  GenericRepository<Product>(db);
            }
            return _genericRepository;
        }
    }}
Run Code Online (Sandbox Code Playgroud)

我遇到2个错误,您可以在下面看到:

  1. 错误CS0311类型'GroceryStore.DataLayer.Context.Product'不能用作通用类型或方法'GenericRepository <TEntity>'中的类型参数'TEntity'。没有从'GroceryStore.DataLayer.Context.Product'到'GroceryStore.DataLayer.Repositories.IGenericRepository <GroceryStore.DataLayer.Context.Product>'的隐式引用转换。

  2. 无法将类型'GroceryStore.DataLayer.Services.GenericRepository <GroceryStore.DataLayer.Context.Product>'隐式转换为'GroceryStore.DataLayer.Repositories.IGenericRepository <GroceryStore.DataLayer.Context.Product>'。存在显式转换(您是否缺少演员表?)

你能告诉我我哪里出问题了吗?为什么?以及我该如何解决?


我有以下声明:

public interface IGenericRepository<TEntity>
    where TEntity: class
{ }

public class GenericRepository<TEntity>
    where TEntity: class, IGenericRepository<TEntity>
{ }
Run Code Online (Sandbox Code Playgroud)

c#

0
推荐指数
1
解决办法
214
查看次数

标签 统计

c# ×1