Entity Framework 6 中的对象查询
Object query in Entity Framework 6
我在使用 ObjectQuery
方法时遇到错误,有人可以帮忙吗?
private void AddProductsToTabbedPanel()
{
foreach (TabPage tp in tabControl1.TabPages )
{
ObjectQuery<TblProduct> filteredProduct = new ObjectQuery<TblProduct>("Select value p from TblProduct as P", csdbe);
foreach (TblProduct tpro in filteredProduct)
{
Button btn = new Button();
btn.Text = tpro.Description;
tp.Controls.Add(btn);
}
}
}
我的逻辑是,它根据 TblProduct 的内容在控制选项卡中添加按钮
但是我得到一个错误:
Argument 2: cannot convert from
'Coffee_Shop.CoffeeShopDatabaseEntities' to
'System.Data.Entity.Core.Objects.ObjectContext'
The best overloaded method match for
'System.Data.Entity.Core.Objects.ObjectQuery.ObjectQuery(string,
System.Data.Entity.Core.Objects.ObjectContext)' has some invalid
arguments
这里真正的问题是使用 entity framework 作为 运行 您的 sql 代码的一种方式,这不是 entity framework 的目的。如果您有 entity framework 附加到您的数据库,只需执行此操作以获取您的实体:
//assuming csdbe is your data context
var filteredProduct = csdbe.TblProduct;
在上面的示例中,您没有过滤查询,只是询问所有查询。要过滤上面的示例,请使用 .Where
var filteredProduct = csdbe.TblProduct.Where(x => x.SomeValue == "yourValue");
现在回答你原来的问题:
参数 2:无法从 'Coffee_Shop.CoffeeShopDatabaseEntities' 转换为 'System.Data.Entity.Core.Objects.ObjectContext'
您得到的异常显示 "csdbe" 是一个 "CoffeeShopDatabaseEntities" 实体。所需的第二个参数是数据上下文。
var filteredProduct = new ObjectQuery<TblProduct>("SELECT VALUE P FROM TblProducts AS P", yourContext);
---您的代码应如下所示
string queryString = @"SELECT VALUE P FROM Tblproducts as P";
foreach (tblproducttype pt in cse.tblproducttypes)
{
ObjectContext context =((IObjectContextAdapter) cse).ObjectContext;
ObjectQuery<tblproduct> filteredproduct = new ObjectQuery<tblproduct>(queryString, context);
foreach (tblproduct tprod in filteredproduct)
{
Button b = new Button();
b.Text = tprod.description;
tp.Controls.Add(b);
}
}
我在使用 ObjectQuery
方法时遇到错误,有人可以帮忙吗?
private void AddProductsToTabbedPanel()
{
foreach (TabPage tp in tabControl1.TabPages )
{
ObjectQuery<TblProduct> filteredProduct = new ObjectQuery<TblProduct>("Select value p from TblProduct as P", csdbe);
foreach (TblProduct tpro in filteredProduct)
{
Button btn = new Button();
btn.Text = tpro.Description;
tp.Controls.Add(btn);
}
}
}
我的逻辑是,它根据 TblProduct 的内容在控制选项卡中添加按钮
但是我得到一个错误:
Argument 2: cannot convert from 'Coffee_Shop.CoffeeShopDatabaseEntities' to 'System.Data.Entity.Core.Objects.ObjectContext'
The best overloaded method match for 'System.Data.Entity.Core.Objects.ObjectQuery.ObjectQuery(string, System.Data.Entity.Core.Objects.ObjectContext)' has some invalid arguments
这里真正的问题是使用 entity framework 作为 运行 您的 sql 代码的一种方式,这不是 entity framework 的目的。如果您有 entity framework 附加到您的数据库,只需执行此操作以获取您的实体:
//assuming csdbe is your data context
var filteredProduct = csdbe.TblProduct;
在上面的示例中,您没有过滤查询,只是询问所有查询。要过滤上面的示例,请使用 .Where
var filteredProduct = csdbe.TblProduct.Where(x => x.SomeValue == "yourValue");
现在回答你原来的问题:
参数 2:无法从 'Coffee_Shop.CoffeeShopDatabaseEntities' 转换为 'System.Data.Entity.Core.Objects.ObjectContext'
您得到的异常显示 "csdbe" 是一个 "CoffeeShopDatabaseEntities" 实体。所需的第二个参数是数据上下文。
var filteredProduct = new ObjectQuery<TblProduct>("SELECT VALUE P FROM TblProducts AS P", yourContext);
---您的代码应如下所示
string queryString = @"SELECT VALUE P FROM Tblproducts as P";
foreach (tblproducttype pt in cse.tblproducttypes)
{
ObjectContext context =((IObjectContextAdapter) cse).ObjectContext;
ObjectQuery<tblproduct> filteredproduct = new ObjectQuery<tblproduct>(queryString, context);
foreach (tblproduct tprod in filteredproduct)
{
Button b = new Button();
b.Text = tprod.description;
tp.Controls.Add(b);
}
}