none
Query() method in DbCollectionEntry<TEntity, TElement> - EF 4.1 RRS feed

  • Question

  • Hey, I tried posting this as a comment on the relevant blog thread (http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx) but for some reason couldn't.

    In the sample :

    // Load the unicorns starting with B related to a given princess    

    context.Entry(princess)
    .Collection(p => p.Unicorns)
    .Query()
    .Where(u => u.Name.StartsWith("B"))
    .Load();

    should Load the matched Unicorns AND attach them to princess. The second part doesn't work. The Unicorns are loaded to the context, but they are not attached to the princess.

    Since DbCollectionEntry<TEntity, TElement> doesn't have a method to modify the actual query that is used to load the collection, I get the impression that this is infact intended behavior - i.e currently, it is not possible to modify the underlying query used.

    Am I correct, or am I missing something ?

     

    Wednesday, June 22, 2011 1:55 PM

All replies

  • Hi, in the article you mentioned above, I don't see that attach them to princess, but it says that " it can be used to load only part of the collection ", so could you explain that?

    Hope this can help you.

     

    L.J

    Monday, June 27, 2011 2:11 PM
  • Hello Anant,

    Thanks for your post.

    As far as I know, Query() method in DbCollectionEntry<TEntity, TElement> Class returns the query that would be used to load this collection from the database. The returned query can be modified using LINQ to perform filtering or operations in the database, such as counting the number of entities in the collection in the database without actually loading them. ---From MSDN Library

    And I also agree with L.J Lincoln's opinion, in that article, see the sample code and the notes, we could find that the Query(0 method only load the the collection matched the filter. If we want to get the attached collection, we should do it manually.

    I hope this can help you.

     

    Have a nice day,

     

     

     


    Jackie Sun [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, June 28, 2011 8:13 AM
    Moderator
  • My bad. I was looking for a way to perform a filtered load AND attach, and read the article (and the section) in that context.

    I still am.

    If I manually set Princess.Unicorns to context.Unicorns.Local.Where(u => u.Name.StartsWith("B")) after performing the aforementioned load, I think change tracking for Princess.Unicorns will not work the way I want. The resultant SQL, if I call SaveChanges() immediatley afterwords will be a bunch of inserts, since I'd have set a null collection to a new one (rather than no changes at all had the attach also taken effect)

    Thoughts ?


    Tuesday, June 28, 2011 10:42 AM