Search code examples
c#entity-frameworkdbcontextquery-builderobjectquery

Using Query Builder Methods against DbContext


I would like to use the "Query Builder Methods" on my DbContext like following:

using (var context = new MyDbContext())
{
    var query = context.MyEntities.Where("Id = @id", new ObjectParameter("id", 1));
}

But it cannot resolve this specific overload of Where. What am I missing here?


Solution

  • Using this solved my problem:

    var objectContext = ((IObjectContextAdapter) context).ObjectContext;
    var query = objectContext.CreateObjectSet<MyEntities>().Where("it.Id = @id", new ObjectParameter("id", 1));