cannot convert from 'P.CoffeeShopDBEntities' to '

本文关键字:CoffeeShopDBEntities to from cannot convert | 更新日期: 2023-09-27 17:53:58

我试图在Visual Studio 2013 (c#)中重新创建代码(我在视频中看到过),这是用VS2008 (c#)编写的:我已经尝试了EF 6和EF 5版本选项相同的结果…搜索网页和MSDN没有运气。

下面是我的代码和注释,显示了我得到的错误:


 private CoffeeShopDBEntities cse = new CoffeeShopDBEntities();
        private void AddProductsToTabbedPanel()
        {
           // using (CoffeeShopDBEntities context = new CoffeeShopDBEntities()) 
     //--found this on msdn..got rid of old but end up w/2 new errors see below
      {
   //the foreach code below goes here
    }

            foreach(TabPage tp in tabControl1.TabPages)
            {
                ObjectQuery<tblProduct> filteredProduct = new ObjectQuery<tblProduct>
                    ("SELECT VALUE P FROM tblProducts AS P", cse);
                //when 'context' used 1)possible mistaken empty statment 
                //2)the name 'context' doesnt exist in the current context
                //when 'cse'used :
                //Error 1 The best overloaded method match for 'System.Data.Objects.ObjectQuery<P.tblProduct>.ObjectQuery(string, System.Data.Objects.ObjectContext)' has some invalid arguments    
                //Error 2 Argument 2: cannot convert from 'P.CoffeeShopDBEntities' to 'System.Data.Objects.ObjectContext'
           foreach (tblProduct tprod in filteredProduct)
                 {
                    Button b = new Button();
                    b.Text = tprod.Description;
                    tp.Controls.Add(b);
                 }
            }
        }

我在Stack Overflow上发现了一个类似的问题,回复说这是一个语法错误,但没有指出在哪里。

cannot convert from 'P.CoffeeShopDBEntities' to '

您正在获得错误,因为ObjectQuery正在寻找EF 4.0及以下版本的ObjectContext。CoffeeShopDBEntities属于DBContext类型,在EF 4.1及以上版本中都有。

我建议将你的查询改为:

List<tblProduct> filteredProducts = cse.tblProducts.ToList();

如果你想阅读更多关于ObjectContext和DBContext的内容,你可以查看这个链接:

http://www.c-sharpcorner.com/UploadFile/ff2f08/objectcontext-vs-dbcontext/

相关文章:
  • 没有找到相关文章