[英]Generic repository issue
我試圖在我的應用程序中實現通用存儲庫模式。 我有兩個接口,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方法中。 有任何想法嗎? 謝謝
您需要添加class
約束
public class RepositoryGlobal<T> : IRepository<T> where T : class, IEntity
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.