開發數據訪問解決方案時,使用通用存儲庫和工作單元模式而非實體框架4.2。我看到一些不正常的行爲。爲了使我的倉庫通用的,我已經使用的DbContext像設置方法:EF DbContext.Set <T>僅限過濾記錄...不起作用
public class MyGenericRepository<T>
{
protected readonly IDbContext context;
public virtual IEnumerable<T> FindBy(Func<T, bool> predicate)
{
return context.GetDbSet<T>().Where(predicate).First();
}
}
其中IDbContext是這樣的:
public interface IDbContext
{
void Commit();
void Attach<T>(T obj) where T : class;
void Add<T>(T obj) where T : class;
DbSet<T> GetDbSet<T>() where T : class;
bool Remove<T>(T item) where T : class;
}
的的DbContext類實現IDbContext爲:
public partial class MyEntities : IDbContext
{
public DbSet<T> GetDbSet<T>() where T : class
{
return this.Set<T>();
}
}
當我執行repository.FindBy(c => c.CompanyId == 45)時,Sql Profiler顯示查詢爲而不是包含任何過濾器(company_id = 45)。該查詢執行Select *。
期待一個過濾器是存在的,我開始研究和跨此次前來,
http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/7d6705ac-f875-4c89-960b-842be6f6e5ed 和 EF DbContext.Set<T> filtered record only
這些線程證實了我的思維過程,但結果是不同的。任何解決方案
謝謝。
非常感謝。現在就像魅力一樣。 –