在下面的类中,如何将DBContext添加为泛型类型参数:
public abstract class Repository<T> : IRepository<T> where T : class
{
private MyDBContext dataContext;
private readonly IDbSet<T> dbset;
protected Repository(IDatabaseFactory databaseFactory)
{
DatabaseFactory = databaseFactory;
dbset = DataContext.Set<T>();
}
protected IDatabaseFactory DatabaseFactory
{
get;
private set;
}
protected MyDBContext DataContext
{
get { return dataContext ?? (dataContext = DatabaseFactory.Get()); }
}
public virtual void Add(T entity)
{
dbset.Add(entity);
}
public virtual void Update(T entity)
{
dbset.Attach(entity);
dataContext.Entry(entity).State = EntityState.Modified;
}
public virtual void Delete(T entity)
{
dbset.Remove(entity);
}
}
以下内容:
public abstract class Repository<C, T> : IRepository<T> where T
它会起作用吗?如何实施?
答案 0 :(得分:1)
你的意思是这样吗?
public abstract class Repository<TContext, TEntity> : IRepository<TContext, TSet>
where TContext : DbContext
where TEntity : class
{
private TContext context;
private readonly IDbSet<TEntity>;
// ...
}
基本上只需用MyDBContext
替换TContext
的所有引用。