locked
What is the correct syntax for "filtered explicit loading"? RRS feed

  • Question

  • WHAT I HAVE:
    Visual Basic 2015, Entity Framework 6.1.3

    MY PROBLEM:
    I read about "Filtered Explicit Loading" on pp. 31-42 of "Programming Entity Framework: DbContext", for using when one wants to load a subset of the contents of a collection navigation property. The following line doesn't compile. It tells me that "Load is not a method of IQueryable(Of Entity2)". What am I doing wrong?

    db.Entry(x).Collection(Function(c) c.Entity2)  _
       .Query().Where(Function(d) d.Id = y.Id).Load()

    where x is an attached entity of type Entity1 (with a collection navigation property of Entity2), and y is an attached entity of type Entity2 (with a collection navigation property Entity1). (In this case, I'm loading a single (pre-existing) entity--but the "Where" test could just as easily be designed to load multiple Entity2 entities.)


    Robert Gustafson

    Monday, March 13, 2017 1:56 PM

Answers

  • Hi RobertGustafson,

    Based on your description, I create a simple and reproduce your issue on my side, it seems that you need to imports ystem.Data.Entity, like this:

    Imports System.Data.Entity
    
    Module Module1
    
        Sub Main()
    
            Using context = New BloggingContext()
                Dim blog = context.Blogs.Find(1)
    
                Dim y = context.Posts.Find(2)
                ' Load the posts with the 'entity-framework' tag related to a given blog 
    
    
                context.Entry(blog).Collection(Function(b) b.Posts).Query().Where(Function(p) p.PostId = y.PostId).Load()
            End Using
    
        End Sub
    
    End Module

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 15, 2017 5:39 AM

All replies

  • Hi RobertGustafson,

    Based on your description, I create a simple and reproduce your issue on my side, it seems that you need to imports ystem.Data.Entity, like this:

    Imports System.Data.Entity
    
    Module Module1
    
        Sub Main()
    
            Using context = New BloggingContext()
                Dim blog = context.Blogs.Find(1)
    
                Dim y = context.Posts.Find(2)
                ' Load the posts with the 'entity-framework' tag related to a given blog 
    
    
                context.Entry(blog).Collection(Function(b) b.Posts).Query().Where(Function(p) p.PostId = y.PostId).Load()
            End Using
    
        End Sub
    
    End Module

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 15, 2017 5:39 AM
  • Thank you! It's easy to forget that various methods only work when specific, otherwise obscure, namespaces/classes are imported. That one statement makes all the difference. Secondary question: When one wishes to delete a relationship between 2 many-to-many entities, it's often necessary to load one entity's navigation-property collection before using Remove so that SaveChanges is aware the change and actually performs the delete. Can the above query-filtered technique be used to load only the relationship item(s) being removed, instead of the entire collection.  It's always convenient to  load only the minimum data needed for an operation. (BTW, I know there are also tricks for deleting a relationship without loading any data. This is kind of an in-between approach.) That is, will this code work?

    Imports System.Data.Entity
    
    '   remove relationship between x and y
    Dim x As Entity1 = attached/queried value
    Dim y As Entity2 = attached/queried value
    '   load only items to be removed from collection
    Context.Entry(x).Collection(Function(c) x.Entity2).Query().Where(Function(d) d.Id = y.Id).Load()
    x.Enity2.Remove(y)
    Context.SaveChanges()
    I think I know the answer, but give it to me anyway, just in case. (I'll mark this post as answered thereafter.)


    Robert Gustafson


    Wednesday, March 15, 2017 5:33 PM
  • Hi RobertGustafson,

    Since it is a new issue, I would suggest that you could post a new thread.

    if the reply is helpful, please mark it as answer, it will be beneficial to other communities who have the similar issue.

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 17, 2017 8:00 AM