locked
Reusing a domain context returns invalid results from a query RRS feed

  • Question

  • In my code-behind, I'm creating a global domain context for a database table using L2S and .NET RIA Services.  I initially issue a query using the domain context that returns all rows in the table.  This query works just fine.  Later in the code-behind, I use the same domain context to issue a new query (without creating a new domain context) that should return a single row.  However, the domain context returns all rows.  Should this be the correct behavior?  When issuing a new query, shouldn't the new results be returned?  BTW, if I create a new domain context for the second query, it works just fine, and returns a single row.

     

    Wednesday, August 19, 2009 7:20 PM

Answers

  • When using the LoadOperation to access the Entities rather than the DomainContext, can I still Add or Delete entites, or is the collection returned by the Domain Context essentially read-only?

    The collection is read only, you can't add or delete entities from it. It is really only useful for binding to read-only data. Personally, I use PagedCollectionView for my binding but I still delete and add directly against the EntityList. This will change when we get a good EntityCollectionView object. What you do is set the filter of the PagedCollectionView (Or EntityCollectionView) to be the same as your load query. That makes it possible to have more entities tracked by your DomainContext than you are actually displaying. It also means that any changes made directly against the EntityList will automatically be shown by the PagedCollectionView.

    Friday, August 21, 2009 3:03 PM

All replies

  • I am curious how you know that the second query is bringing back all rows. Are you using Fiddler to see what is going across the wire?

    Wednesday, August 19, 2009 7:28 PM
  • Is the issue where you're looking for the data? If you look at context.MyEntities, you'll see it aggregates all the data from the first and second calls. However if you just want to see the data returned from a single query, you can look at loadOperation.Entities.

      LoadOperation op = context.Load<MyEntities>(context.GetMyEntitiesQuery(), GetMyEntitiesQueryCompleted, null)

    op.Entities won't be populated until the callback, but you can bind to it right away. In the callback, you should see that it contains only the results of the latest query and not the aggregated results of multiple queries.

    Kyle

    Wednesday, August 19, 2009 9:12 PM
  • op.Entities won't be populated until the callback, but you can bind to it right away.

    This raises some questions:

    - are these the server side entities, or the server side entities merged with potential local changes? So each entity instance in "op.Entities" is exactly the same as the entity in the complete set of the domain context, right?

    - I assume this means that you extend the lifetime of the load operation object this way, since Entities is exposed by the LoadOperation. Is that an issue?

    Thursday, August 20, 2009 2:37 AM
  • So each entity instance in "op.Entities" is exactly the same as the entity in the complete set of the domain context, right?

    Correct

    I assume this means that you extend the lifetime of the load operation object this way, since Entities is exposed by the LoadOperation. Is that an issue?

    No, you are holding reference on the ReadOnlyObservableCollection exposed by the LoadOperation, not to the LoadOperation itself. The collection doesn't have any references back to the LoadOperation so the LoadOperation will GC as soon as you get rid of any references you have to it.

    Thursday, August 20, 2009 7:57 AM
  • When using the LoadOperation to access the Entities rather than the DomainContext, can I still Add or Delete entites, or is the collection returned by the Domain Context essentially read-only?

    Friday, August 21, 2009 2:46 PM
  • When using the LoadOperation to access the Entities rather than the DomainContext, can I still Add or Delete entites, or is the collection returned by the Domain Context essentially read-only?

    The collection is read only, you can't add or delete entities from it. It is really only useful for binding to read-only data. Personally, I use PagedCollectionView for my binding but I still delete and add directly against the EntityList. This will change when we get a good EntityCollectionView object. What you do is set the filter of the PagedCollectionView (Or EntityCollectionView) to be the same as your load query. That makes it possible to have more entities tracked by your DomainContext than you are actually displaying. It also means that any changes made directly against the EntityList will automatically be shown by the PagedCollectionView.

    Friday, August 21, 2009 3:03 PM
  • You need to enter the folowing line just before you call the load for the new query. MyGlobalContext.MyEntities.EntityContainer.Clear(); Another option is to use the overloaded "Load" with the option "LoadBehavior.RefreshCurrent". MyGlobalContext.Load(MyGlobalContext.GetMyEntitiesQuery(), LoadBehavior.RefreshCurrent, true);
    Monday, May 31, 2010 8:01 AM
  • I had the exact same problem where the entity values returned by the query were never refreshed and calling MyGlobalContext.MyEntities.EntityContainer.Clear() did the trick. Thanks.

    Saturday, August 28, 2010 12:40 AM