繁体   English   中英

对象查询 <T> 实体框架6中的等效项

[英]ObjectQuery<T> equivalent in Entity Framework 6

使用Entity Framework 4(旧版),我可以使用ObjectQuery进行查询,例如:

// where Product is a table in a sample 
// database created with sql management studio
// Product table has a foreign key pType to another table p_type and there is a pType key there
// pType is of type int and another column that is called description which describes the pType
// 1 would be shoes 2 would be hats etc.
// filteredCombobox is data boung pType(displaymember) and Description(valuemember) 
// dbcontext is the database Entity

//this event below is the SelectionChangeCommit
        private void listToBeFiltered(object sender, EventArgs e)
{
    ObjectQuery<Product> itemsToBeFiltered = new ObjectQuery<Product>(
    "Select Value c FROM Product AS c WHERE c.pType = " + filterCombobox.SelectedValue, dbcontext);

    dataGridView1.DataSource = itemsToBeFiltered;
}

因此,当我在组合框中选择鞋子时,网格应仅显示鞋子pType(鞋子描述为1)。

我想知道什么是上面代码的EF6等效项。 被困了两个星期。 任何帮助将不胜感激。 谢谢朋友

尽管不推荐使用,也不推荐使用标准方法,但可以将DbContext转换为ObjectContext并使用它:

using (var dbContext = new MyContext())
{
    var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
    var itemsToBeFiltered = objectContext.CreateQuery<Product>("sql here...", params);
}

编辑:只是链接到OP的下一个问题: 实体框架6 + C#传递combobox.SelectedValue作为context.CreateQuery的参数我缺少一些简单的东西?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM