【发布时间】:2017-11-17 00:55:52
【问题描述】:
在这里,我有一个从教程页面复制的通用 Repository 类,但具体而言,我的问题在于最后两个函数。在我的项目中,我有几个从 CRUDProperties 类继承的目录实体,并且在所有这些实体中都有一个属性“Activo”,我目前想要做的是,如果实体从 CRUDProperties 类继承,我将获得所有具有 Activo 属性的实体是的,如果它们不从该类继承,它只会获取所有实体。但是编译器会抛出一个错误,说明 T 已经定义。我该怎么办?
public class Repository<T> where T : class
{
private readonly projectEntities context;
private IDbSet<T> entities;
string errorMessage = string.Empty;
public Repository(projectEntities context)
{
this.context = context;
}
public T GetById(object id)
{
return context.Set<T>().Find(id);
}
// This is the function that throws me a compilation error
public virtual IList<T> GetAll<T>() where T : CRUDProperties
{
return context.Set<T>().Where(c => c.Activo).ToList();
}
public virtual IList<T> GetAll()
{
return context.Set<T>().ToList();
}
}
【问题讨论】:
-
你在周围的类
Repository<T>中已经有了一个类型参数T。如果您想为该类中的方法添加新的/不同类型的参数,那么您需要为该类型参数指定一个不同的名称。 -
@bassfader 如果我希望它尽可能通用,可以分配任何其他字母?
标签: c# entity-framework repository-pattern