3
我想在我的应用程序中实现通用存储库模式。我有两个接口,IEntity和IRepository:通用存储库问题
IEntity:
public interface IEntity
{
int Id { get; set; }
}
IRepository:
public interface IRepository<T> where T : IEntity
{
void AddOrUpdate(T ent);
void Delete(T ent);
IQueryable<T> GetAll();
}
现在我想做一个普通的RepositoryGlobal类,但我得到这个错误:
The type 'T' must be a reference type in order to use it as parameter 'T' in the generic type or method
这是我的代码如下所示:
public class RepositoryGlobal<T> : IRepository<T> where T : IEntity
{
public RepositoryGlobal(DbContext _ctx)
{
this._context = _ctx;
}
private DbContext _context;
public void Add(T ent)
{
this._context.Set<T>().Add(ent);
}
public void AddOrUpdate(T ent)
{
if (ent.Id == 0)
{
//not important
}else
{
//for now
}
}
public void Delete(T ent)
{
}
public IQueryable<T> GetAll()
{
return null;
}
}
该错误出现在RepositoryGlobal类的Add方法中。 任何想法? 感谢
添加由于在'DbContext'类Set'方法'定义的限制:)'公共虚拟DbSet设置(其中TEntity:类' –