none
Entity Framework - working with in memory context RRS feed

  • Question

  • Hi,

    I am not sure how to phrase the question so I think the easiest thing for me to do is to describe the scenario and what it is I am wanting to accomplish. Note that I am using Visual Studio 2010 so no T4 templates or new features from EF5 are being used.

    I have a Manager class with a local instance of a data context object that has been derived from ObjectContext class (your basic implementation here without T4 templates). My data context object has a few dozen tables. What I want to do is to add several records to a couple of the tables using my data context object instance and then have other methods query across these records in addition  to what has already been persisted to the database BEFORE persisting anything to the database. This way I can make a decision whether to persist everything at once at the end based on the results of the helper methods or add additional data to the same transaction etc.

    Again, my scenario:
    1. Create a new data context object (derived from implementation of ObjectContext using designer).
    2. Add records to the tables in the data context object using .AddToXXX() or .TableName.AddObject() (I have tried both)
    3. Query these tables from the data context object but have it now also include the in memory additions ex: .TableName.Count() to include new records
    4. Persist all changes... maybe... depending on results from actions in previous step.

    Is this possible? Maybe there is some work around to achieve this or maybe I am not looking in the wrong place?

    Thank you in advance!

    -Igor


    -Igor

    Tuesday, January 29, 2013 1:11 AM

Answers

  • I found the answer. I believe the best way to do this is to query the ObjectStateManager and then get the additions that match the type I am looking for. I created a little generic helper method. I then have to combine this in memory list with what I filter out of the database and use the combined list. This involves a little work but it is better than passing around countless lists.

    Here is the code for retrieving in memory objects if anyone is interested.

    public IList<T> GetInMemoryAdditions<T>(ObjectContext objectContext) where T : class, new()
    {
       var additions = objectContext.ObjectStateManager
          .GetObjectStateEntries(EntityState.Added);
    
       return additions.Select(addition => addition.Entity)
          .OfType<T>().ToList();
    }
    



    -Igor

    • Marked as answer by IWolbers Tuesday, January 29, 2013 6:07 PM
    Tuesday, January 29, 2013 6:07 PM

All replies

  •   

    The only issue I see is that after step 2 but before step 3 you need to save changes to the database because if you query the database without saving the new records they will not be returned in the query results.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, January 29, 2013 4:44 AM
  • Thank you for taking your time to respond Fernando but I realize that if you persist the changes to the database then the records will come back in the queries against the ObjectContext. As I described above I do not want to persist the changes yet, in fact the changes might not need to be persisted at all so persisting and then deleting again is not an option.

    So.... looks like at this time the answer is 'not possible'? Guess I will have to create a Lists of records from the database combine them with in memory collections and then use those to query from, seems like pain in the butt though.
    Anyone else have a better suggestion other than persisting the records to the DB? Maybe there is an easy way to retrieve the in-memory records from the ObjectContext only so I don't have to start passing around new Lists/Collections as parameters?


    -Igor

    Tuesday, January 29, 2013 11:49 AM
  • I found the answer. I believe the best way to do this is to query the ObjectStateManager and then get the additions that match the type I am looking for. I created a little generic helper method. I then have to combine this in memory list with what I filter out of the database and use the combined list. This involves a little work but it is better than passing around countless lists.

    Here is the code for retrieving in memory objects if anyone is interested.

    public IList<T> GetInMemoryAdditions<T>(ObjectContext objectContext) where T : class, new()
    {
       var additions = objectContext.ObjectStateManager
          .GetObjectStateEntries(EntityState.Added);
    
       return additions.Select(addition => addition.Entity)
          .OfType<T>().ToList();
    }
    



    -Igor

    • Marked as answer by IWolbers Tuesday, January 29, 2013 6:07 PM
    Tuesday, January 29, 2013 6:07 PM