none
Is there a way to clear Local without marking entities as deleted (DbContext 4.1)? RRS feed

  • Question

  • Hi!

    I'm using DbContext over existing ObjectContext and everything seems to work fine so far, but I can't figure out how to clear Local without marking entities as deleted. 

    When I execute following code (this is just example code to illustrate the problem, not real world situation):

    MyEntities ctx = new MyEntities();
    DbContext db = new DbContext(ctx, true);
    
    ctx.Cars.Load();
    db.Set<Car>().Local.Clear();
    
    ctx.Cars.Where(c=>c.CarType = "Caravan").Load();
    bindingSource.DataSource = db.Set<Car>().Local.ToBindingList();
    

    there is no data in bindingSource. My guess is Clear() marks all entities as deleted and doesn't show them any more in Local. 

    Is there a way to clear Local so I can get another set of data without currently loaded entities being marked as deleted?

    Thanks, 

    Mario

    Tuesday, December 13, 2011 7:19 PM

Answers

  • I've found way to do it. Instead of Local.Clear(), I'm doing this:

     

    db.Set<Car>().Local.ToList().ForEach( x=>
       {
          db.Entry(x).State = EntityState.Detached;
          x = null; // <- this doesn't seem to be required for garbage collection
       }

     

    This clears .Local and enables loading of new set of entities. Not very clean solution, but it works. If anyone knows a better way, please let me know. 

    Thanks


    Wednesday, December 14, 2011 12:57 PM
  • Hi Mario,

    I think you method is good, you can use a new instance of DbContext to show other records.

    Have a nice day.


    Alan Chen[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.

    Thursday, December 15, 2011 1:31 PM
    Moderator

All replies

  • I've found way to do it. Instead of Local.Clear(), I'm doing this:

     

    db.Set<Car>().Local.ToList().ForEach( x=>
       {
          db.Entry(x).State = EntityState.Detached;
          x = null; // <- this doesn't seem to be required for garbage collection
       }

     

    This clears .Local and enables loading of new set of entities. Not very clean solution, but it works. If anyone knows a better way, please let me know. 

    Thanks


    Wednesday, December 14, 2011 12:57 PM
  • Hi Mario,

    I think you method is good, you can use a new instance of DbContext to show other records.

    Have a nice day.


    Alan Chen[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.

    Thursday, December 15, 2011 1:31 PM
    Moderator
  • I tried this, and it works, except that if you then want to reload detatched records in a later query, they won't load.  You'll have to go through your detatched records and reset the State.  Other then that, this works great.

    It would be great if, however, you could simply reset the dbSet<T> without having to reset your entire DbContext.

    Tuesday, October 30, 2012 9:05 PM