none
How do I retrieve the objects that are already loaded in Entity Framework in lists per type? RRS feed

  • Question

  • I load up my objects from a query, now, I want to get a Collection<Type> of the objects loaded from my query.  If I access the DbSets, they load up all the data I do not want.  Is there a way to tell the DbSets to not load additional data from the DB and only give me the objects that are currently loaded?


    Michael Stevens

    Monday, May 12, 2014 7:34 PM

Answers

  • I had to use the ChangeTracker to get the Entries of those types.  This way, the Entities that are loaded to be handled by the change tracker could be accessed.

    TestModelContainer tmc = new TestModelContainer();

    [LINQ Query Here]

    use the .Load() to load the results.

    foreach (DbEntityEntry<User> dbp in tmc.ChangeTracker.Entries<User>())
    {

    .. Do Operation per loaded entry

    }


    Michael Stevens

    • Marked as answer by Mike Stevens Tuesday, May 13, 2014 5:15 PM
    Tuesday, May 13, 2014 5:14 PM

All replies

  • Are you talking about the loading of related entities? If you don't want to load any related entities, you could turn off lazy loading by setting the LazyLoadingEnabled property of the Configuration object of the DbContext to false:

    using(var context = new YourContext())
    {
    context.Configuration.LazyLoadingEnabled = false; 
    ...load entities..
    }

    Lazy loading is enabled by default. Please refer to the following page for more information about the loading of related entities in Entity Framework: http://msdn.microsoft.com/en-us/data/jj574232.aspx

    If you only want to load a subset of the entities within a DbSet, you should specify a where clause that specifies which entities you want to load:

    var items = context.YourEntitySet.Where(e => e.SomeProperty.Equals("some value")).ToList();
    

    Monday, May 12, 2014 8:26 PM
  • After I have loaded all my entities.  I want to cycle through the entities that I just loaded, per type.

    HistoryItem history = from h in context.HistoryItems.Include("HistType").Include("User").Include("RelatedHistoryItems").Include("RelatedHistoryItems.HistoryItem.User").Include("RelatedHistoryItems.HistoryItem") where h.HistType.Text == "Service Started" select h;

    context.Set<User>() <--- accessing this grabs every user in the system.

    context.Set<HistoryItems> <--- I need all the History Items that were loaded from the base select and from the related items.  This returns me all the ones on the server.

    I need something that does the same but only gets the users and history items that have been already loaded from the DB using the LINQ Query.  Notice the related History Items.  I need the full list of type HistoryItem that was loaded from the DB and full list of type User.  Thanks in advance. 


    Michael Stevens

    Tuesday, May 13, 2014 12:44 PM
  • There is a reason I need to do it this way, changing the LINQ query would fix the get me what I want in this specific example but, I'm using this as only an example of the problem I am experiencing in a smaller way.  I need all the objects loaded into the graph per type.  The solution I am working with has 150+ types loaded into the graph but, they are in this hierarchical map of related objects.  I just need to cycle down per type after the base object and all realted objects are loaded to the graph.

    Michael Stevens

    Tuesday, May 13, 2014 12:57 PM
  • I had to use the ChangeTracker to get the Entries of those types.  This way, the Entities that are loaded to be handled by the change tracker could be accessed.

    TestModelContainer tmc = new TestModelContainer();

    [LINQ Query Here]

    use the .Load() to load the results.

    foreach (DbEntityEntry<User> dbp in tmc.ChangeTracker.Entries<User>())
    {

    .. Do Operation per loaded entry

    }


    Michael Stevens

    • Marked as answer by Mike Stevens Tuesday, May 13, 2014 5:15 PM
    Tuesday, May 13, 2014 5:14 PM