ObjectQuery<T> 相当于 Entity Framework 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;
}
所以当我 select 组合框中的鞋子时,网格应该只显示鞋子 pType,它是带有鞋子描述的 1。
我想知道上面代码的 EF6 等价物是什么。卡了2个星期。
任何帮助将不胜感激。谢谢朋友
虽然不推荐或标准方法,但您可以强制转换 DbContext
以获得 ObjectContext
并使用它:
using (var dbContext = new MyContext())
{
var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
var itemsToBeFiltered = objectContext.CreateQuery<Product>("sql here...", params);
}
编辑: 只是链接到 OP 的下一个问题:
使用 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;
}
所以当我 select 组合框中的鞋子时,网格应该只显示鞋子 pType,它是带有鞋子描述的 1。
我想知道上面代码的 EF6 等价物是什么。卡了2个星期。 任何帮助将不胜感激。谢谢朋友
虽然不推荐或标准方法,但您可以强制转换 DbContext
以获得 ObjectContext
并使用它:
using (var dbContext = new MyContext())
{
var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
var itemsToBeFiltered = objectContext.CreateQuery<Product>("sql here...", params);
}
编辑: 只是链接到 OP 的下一个问题: