none
Why Is CreateQuery No Longer Available - Has Namespace moved? RRS feed

  • Question

  • Hi All

    I have just upgraded a web project to use Visual Studio 2012. It now uses entityframeworks 5 and one line is no longer worling.  I am trying to generate an entityframeworks query on the fly using the following :

    List<ObjectParameter> oList = new List<ObjectParameter>();

    string queryString = @"..... ";  //

    //put the queryString and paramList into the entity frameworks machine
    using (myEntities context = new myEntities())
    {
    //convert the list to array
    ObjectParameter[] paramList = oList.ToArray();
    ObjectQuery<SaleAd> adQuery = context.CreateQuery<SaleAd>(queryString, paramList);
    etc;
    }

    I receive a build error saying myEntities does not contain a definition for CreateQuery ...are you missing a using directive or assembly reference.

    Anybody know which one I might be missing?

    Sunday, October 7, 2012 3:41 PM

Answers

  • Hi Al_Robson;

    The reason is that earlier versions used ObjectContext as the context type where in EF 5 uses DbContext. To get around the issue get a reference to the ObjectContext and use that. For example try the following.

    using (myEntities context = new myEntities())
    {
    	//convert the list to array
    	ObjectParameter[] paramList = oList.ToArray();
    	// Replace this statement with the one below it.
    	// ObjectQuery<SaleAd> adQuery = context.CreateQuery<SaleAd>(queryString, paramList);
    	ObjectQuery<SaleAd> adQuery = ((IObjectContextAdapter) context).ObjectContext.CreateQuery<SaleAd>(queryString, paramList) );
    	etc;
    }

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Alexander Sun Tuesday, October 9, 2012 6:33 AM
    • Marked as answer by Alexander Sun Tuesday, October 23, 2012 8:38 AM
    Sunday, October 7, 2012 5:13 PM