none
Issue with EntityDataSource's QueryCreated event after converting to EF 4.1: QueryCreated event returned a query of type 'DbQuery`1' when type 'ObjectQuery`1' is required RRS feed

  • Question

  • I recently converted my project to EF 4.1. I have an aspx page using an EntityDatasource. The datasource defines Querycreated event like this:

    protected void EntityDataSource_TxRequest_QueryCreated(object sender, QueryCreatedEventArgs e)
    
     {
    
      var q = e.Query.Cast<TxRequest>();
    
    
    
      SmartUREntities db = new SmartUREntities();
    
    
    
      q = db.TxRequests.Where(t => t.DateOfCreation <= DateTime.Today.AddDays(-30));
    
    
    
      e.Query = q;
    
     } 

    This code throws an exception:

    QueryCreated event returned a query of type 'DbQuery`1' when type 'ObjectQuery`1' is required




    Thursday, July 14, 2011 4:18 AM

Answers

  • Hi andy,

    Welcome!

    To tell your truth, I'm not familiar with EntityDatasource, base on my test, it works for EF4(edmx), but I couldn't set Named Connection in EF4.1 code first. I do some research it seems it doesn't support DbContext.

    Have a nice day.

     

     


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, July 15, 2011 6:35 AM
    Moderator
  • Per Rune Gulbrandsen, this is not possible. Here is his comment from another post which certainly jives with my actual experience trying to convert to the expected type...

    "I haven't used the EntityDataSource myself, so I'm not familiar with the use of it. But, if I'm not totally wrong, EntityDataSource wants to work directly against a valid EF ObjectQuery. However, the .Local will return a ObservableCollection to you wich cannot be converted into a ObjectQuery (ObjectQueries is just a definition of the query to run, not the actual result of a query). So, to make your code work, you should not use EntityDataSource, but ObjectDataSource instead, this will solve your problem."

    Tuesday, July 26, 2011 5:05 PM

All replies

  • Hi andy,

    Welcome!

    To tell your truth, I'm not familiar with EntityDatasource, base on my test, it works for EF4(edmx), but I couldn't set Named Connection in EF4.1 code first. I do some research it seems it doesn't support DbContext.

    Have a nice day.

     

     


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, July 15, 2011 6:35 AM
    Moderator
  • Hi andy,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, July 18, 2011 3:58 AM
    Moderator
  • Andy, did you ever get this to work? I am in the same boat!

    I would love to see an answer to this...

    Tuesday, July 26, 2011 2:10 AM
  • Hi andy,

    I used DbContext generator item(.tt) to genrate DbContext.

    The result type of the query that you assign to the e.Query parameter has to be the same type as the return type that is specified during the creation of the control, otherwise an exception will occur.(http://msdn.microsoft.com/en-us/library/ee404748.aspx)

     

    protected void EntityDataSource_TxRequest_QueryCreated(object sender, QueryCreatedEventArgs e)
     {
     var q = e.Query.Cast<TxRequest>();
     //SmartUREntities db = new SmartUREntities();
     var test = q.Where(t => t.DateOfCreation <= DateTime.Today.AddDays(-30));
    e.Query = test;
     } 
    

    Please let me know your feedbacks.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, July 26, 2011 9:52 AM
    Moderator
  • Alan, thanks! I just installed EF 4.1 Update 1. THis allows a local view. Per my understanding, that is the "database" view which I also can get to work. I'm hoping to get the "local" in memory view to work in this same situation. I would be forever indebted if you can show how to make that work...I've tried every variation I can think of or that I found compiled (what I call the monkey and typewriter approach ;-).

    In the following code, the commented out "database" query works with EntityDataSource but the "local" version does not. The results are returned just fine but the EntityDataSource chokes on them with the following message -

    "QueryCreated event returned a query of type 'EnumerableQuery`1' when type 'ObjectQuery`1' is required."

    Any suggestions? Thanks so much! Dave

            void MyEntityDataSource_QueryCreated(object sender, QueryCreatedEventArgs e)
            {
                // bring all of the Product objects into memory
                mObjectContext.Products.Load();

                // this works but get the database view
                //var products = e.Query.Cast<TestQueryCreated.Product>();
                //e.Query = (from prod in products where prod.ListPrice < 10 select prod);

                // this yields the proper local results but get this error from the EntityDataSource
                // "QueryCreated event returned a query of type 'EnumerableQuery`1' when type 'ObjectQuery`1' is required."
                System.Linq.IQueryable<TestQueryCreated.Product> prods =
                    (from prod in mObjectContext.Products.Local where prod.ListPrice < 10 select prod).AsQueryable();
                e.Query = prods;

                return;
            }

    Tuesday, July 26, 2011 12:32 PM
  • Per Rune Gulbrandsen, this is not possible. Here is his comment from another post which certainly jives with my actual experience trying to convert to the expected type...

    "I haven't used the EntityDataSource myself, so I'm not familiar with the use of it. But, if I'm not totally wrong, EntityDataSource wants to work directly against a valid EF ObjectQuery. However, the .Local will return a ObservableCollection to you wich cannot be converted into a ObjectQuery (ObjectQueries is just a definition of the query to run, not the actual result of a query). So, to make your code work, you should not use EntityDataSource, but ObjectDataSource instead, this will solve your problem."

    Tuesday, July 26, 2011 5:05 PM
  • It is possible, conenct your entitydatasource to the correct context on creation.  Then use the same SecurityEFConn in your query for filtering.  If the filter requires data outside of the context create List<objects> and apply them into the Linq. 

                                   

            public ServicesEntities ServicesDbContext
            {
                get { return _servicesdbContext ?? (_servicesdbContext = new ServicesEntities()); }
            }

    protected void EntityDataSource_ContextCreating(object sender,

    EntityDataSourceContextCreatingEventArgs e)
            {
    e.Context = (ServicesEntities  as IObjectContextAdapter).ObjectContext;

            }

           public virtual void EntityDataSourceErrorLog_OnQueryCreated(object sender, QueryCreatedEventArgs e)
            {
                List<decimal> serviceFilters = ServiceFilters;

                if (serviceFilters.Count > 0)
                {
                    e.Query = from errorLog in e.Query.Cast<ERRORLOGANDSERVICELOG_VIEW>()
                              join idsServicesInstance in ServicesDbContext.IDS_SERVICES_INSTANCE
                                  on errorLog.INSN_ID equals idsServicesInstance.INSN_ID
                              join services in ServicesDbContext.IDS_SERVICES
                                  on idsServicesInstance.SRV_ID equals services.SRV_ID
                              where serviceFilters.Contains(services.SRV_ID)
                              orderby errorLog.ERR_LOG_ID
                              select errorLog;
                }
                else
                {
                    e.Query = from errorLog in e.Query.Cast<ERRORLOGANDSERVICELOG_VIEW>()
                              orderby errorLog.ERR_LOG_ID
                              select errorLog;
                }
            }


    Friday, October 11, 2013 8:52 PM