none
EF DbContext.Set<T> filtered record only ... problem. RRS feed

  • Question

  • While developing a data access solution that utilizes generic repository and unit of work pattern over entity framework 4.2. I am seeing some abnormal behavior. To make my repositories generic, I have utilized the Set<T> method of DbContext like:

    public class MyGenericRepository<T> 
    {

    protected readonly IDbContext context;

    public virtual IEnumerable<T> FindBy(Func<T, bool> predicate)
            {
                return context.GetDbSet<T>().Where(predicate).First();
            }

    }

    where IDbContext is like: 

    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;
        }

    The DbContext class implements the IDbContext as: 

    public partial class MyEntities : IDbContext

    {

    public DbSet<T> GetDbSet<T>() where T : class
            {
                return this.Set<T>();
            }

    }

    When I do repository.FindBy(c => c.CompanyId == 45), Sql Profiler shows the query to NOT contain any filter (company_id = 45). The query does a Select *.

    Expecting a filter to be present, i started researching and came across this,

    http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/7d6705ac-f875-4c89-960b-842be6f6e5ed

    The thread confirms my thought process but the results are different. Any solutions?

    Thanks,

    Umair.


    • Edited by Umair Ishaq Tuesday, November 6, 2012 8:45 PM
    Tuesday, November 6, 2012 8:44 PM

Answers

All replies