error convertion from 'DataBaseEntities' to 'System.Data.Objects.ObjectContext'

628 Views Asked by At

I am trying to develop a Sales and Inventory system using VS2015 c#. However, today I am getting errors. Here's my code:

 public partial class Form4 : Form
{

    private BindingList<tblProduct> products = new BindingList<tblProduct>();

    private db_22VapeStreetEntities cse = new db_22VapeStreetEntities();

    public Form4()
    {
        InitializeComponent();

        lstProductsChosen.DataSource = products;
        lstProductsChosen.DisplayMember = "Description";

        createTabbedPanel();

    }

    private void AddProductsToTabbedPannel()
    {
        foreach (TabPage tp in tabControl1.TabPages)
        {
            ObjectQuery<tblProduct> filteredProduct = new ObjectQuery<tblProduct>("SELECT VALUE P FROM tblProducts AS P", cse);

        }

The error is when I call the "cse" here --> ObjectQuery<tblProduct>("SELECT VALUE P FROM tblProducts AS P", cse); I am getting

Argument 2: cannot convert from 'inventorysystem.db_22VapeStreetEntities' to 'System.Data.Objects.ObjectContext'

I tried searching the problem but I can't see any solutions.

1

There are 1 best solutions below

4
Chris Schaller On

From the little code you have posted, it is possible your entity context is actually a DBContext.

If that is the case, then try this

    foreach (TabPage tp in tabControl1.TabPages)
    {
        ObjectQuery<tblProduct> filteredProduct = new ObjectQuery<tblProduct>("SELECT VALUE P FROM tblProducts AS P", ((IObjectContextAdapter)cse).ObjectContext);
    }

If this helps though you should investigate newer ways to parameterize your queries with conditional filters using DBContext. I presume for now you have other reasons for staying with ObjectContext patterns.

If you have a DBContext, try this Linq statement:

IQueryable<tblProduct> filteredProduct = cse.tblProducts.AsQueryable();

Or as you have omitted the actual filter syntax that you are using we can try something like this:

string filterValue = "value1";
IQueryable<tblProduct> filteredProduct = cse.tblProducts.Where(p => p.FilterColumn == filterValue);