如何通过依赖注入向EF添加过滤器

时间:2012-04-13 02:46:47

标签: entity-framework dependency-injection

我想首先在ef代码中为linq添加一些条件(where)。

using (var context = new Context())
{
            var u= context.Users;
            **u.where(my where condition)**
        }

有没有办法让我进入所有选择, 例如:之前选择?谢谢

1 个答案:

答案 0 :(得分:1)

最简单的方法是在DbContext上创建一个包装器。

public class EfWrapper:Context
{
 private DbContext _dbContext;

  public EfWrapper(DbContext context){
    _dbContext=context;
  }

  public IEnumerable <User> Users{
     get
     {
        return _dbContext.Users.Where(my where condition);
     }
 }


}