【问题标题】:Using Query Builder Methods against DbContext对 DbContext 使用查询生成器方法
【发布时间】:2013-05-04 14:57:11
【问题描述】:

我想在我的DbContext 上使用“Query Builder Methods”,如下所示:

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

但它无法解决 Where 的特定重载问题。我在这里错过了什么?

【问题讨论】:

    标签: c# entity-framework dbcontext query-builder objectquery


    【解决方案1】:

    使用它解决了我的问题:

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

    【讨论】:

    • 没那么难:context.MyEntities.Where(x=&gt;x.Id == 1);
    • 我当然知道,只是尝试构建动态查询。
    猜你喜欢
    • 2017-07-21
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    相关资源
    最近更新 更多