0

I'm getting an error with the ObjectQuery method, can someone help?

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);
                }
            }
        }

my logic here is that it adds button in control tab based on what is the content of TblProduct

But I got an error:

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

LuluErwin
  • 39
  • 1
  • 9
  • what is `csdbe` type? – Taher Rahgooy Aug 17 '15 at 08:27
  • *Most likely* you're using a `DbContext` oriented approach (`csdbe` descendes from `DbContext` - not `ObjectContext` - right?), which really doesn't support `ObjectQuery` anymore. This is very old EF code - you should upgrade this to a more recent method – marc_s Aug 17 '15 at 10:04
  • Do you have any link on the recent method? if you have this will help me a lot. Again thanks for the response. – LuluErwin Aug 17 '15 at 10:56
  • Why do you use `ObjectQuery` and query strings? There can be good reasons for this, but if you can do without, all the better. If not, I wonder if you're aware of the fact that `ObjectQuery` produces *tracked* entities, while newer `DbContext` methods like `SqlQuery` don't. – Gert Arnold Aug 17 '15 at 12:11

2 Answers2

0

To start the true problem here is using entity framework as a way to run your sql code, this is not what entity framework is for. If you have entity framework attached to your database just do this to get your entities:

//assuming csdbe is your data context
var filteredProduct = csdbe.TblProduct;

In your example above you are not filtering your query, just asking for all of them. To filter the example above use .Where

var filteredProduct = csdbe.TblProduct.Where(x => x.SomeValue == "yourValue");

Now for your original question:

Argument 2: cannot convert from 'Coffee_Shop.CoffeeShopDatabaseEntities' to 'System.Data.Entity.Core.Objects.ObjectContext'

It Appears by the exception you are getting that "csdbe" is a "CoffeeShopDatabaseEntities" entity. The second parameter required is a data context.

var filteredProduct = new ObjectQuery<TblProduct>("SELECT VALUE P FROM TblProducts AS P", yourContext);
Stephen Brickner
  • 2,584
  • 1
  • 11
  • 19
  • Hello Stephen, Thanks for this.. I think.. I need to learn more.. regarding the new entity framework. Thank you..again – LuluErwin Aug 17 '15 at 12:43
  • No problem. I think as you learn more about entity framework or any other ORM for that matter you'll find that it's actually easier to use than writing SQL any way. – Stephen Brickner Aug 17 '15 at 12:54
  • Can you please help me solve, as of this moment... don't have time to read it more.. as I need to finish this project.. by tomorrow.. Here is my code. – LuluErwin Aug 17 '15 at 13:03
  • private void AddProductsToTabbedPanel() { foreach (TabPage tp in tabControl1.TabPages ) { ObjectQuery filteredProduct = new ObjectQuery("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); } } } – LuluErwin Aug 17 '15 at 13:07
  • The fix for it is already in my answer. Change csdbe at the end of this line: ("Select value p from TblProduct as P", csdbe); to your data context so it looks more like this: ("Select value P from TblProduct as P", context); Replace "context" with the name of your data context. You are passing your query into an entity and not the data context. – Stephen Brickner Aug 17 '15 at 13:12
  • Can you update your question to show your data context then I can help you fix your code, thanks. – Stephen Brickner Aug 17 '15 at 13:13
  • it adds button in tabbed panel based on the what is inside of database product. – LuluErwin Aug 17 '15 at 13:33
  • I need to see where your data context is and what csdbe is. Can you add those? – Stephen Brickner Aug 17 '15 at 13:54
  • @StephenBrickner Nothing wrong with using EF to run SQL. Especially in DDD for cross-aggregate SQL queries. – Chalky Nov 12 '15 at 05:26
0

---your code should look like this

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);
    }
}
Matze
  • 5,100
  • 6
  • 46
  • 69
sam
  • 1