Core Entity: how to get the latest state of the committed relationship for nav. property

Suppose there is a model

class Item{
   public int Id {get; set;}
}

class ItemHolder{
   public int Id {get; set;}
   ICollection<Item> Items {get; set;}
}

      

Let's assume we have code like this:

var holder = new ItemHolder{Id = 123}
holder.Items.Add(new Item{Id=2});
holder.Items.Add(new Item{Id=5});
holder.Items.Add(new Item{Id=8});
db.Save(holder)
db.SaveContext();

      

after that, in some other area, we change the navigation properties as follows:

var holder = db.Holders.Single(i => i.Id = 123) // no navigation property load
holder.Items.Add(new Item{Id=3});
holder.Items.Add(new Item{Id=5});
holder.Items.Add(new Item{Id=7});

      

Now I want to load previously saved items to check which ones still exist in the item, which items were recently added, which ones were removed. I tried to use explicit loading with

db.Entry(holder).Collection(h => h.Items).Load()

      

but it only adds previously saved items in holder.Items

without duplicates and any cleanup, so after that it is no longer possible to understand which items were saved earlier and which items were added during the current transaction. But I need to know that in order to manually set the state for entities, for example, if they were saved previously, but does not exist in the current elements, and not in State.Deleted

, if it was previously saved and is still in the current one, this State.Unmodified

is if they are not were previously saved but exist in current - it State.Added

.

from example: 2.8 - removed, 5 - unmodif, 3.7 - added

So my problem is to get the collection of previously saved items holder.Items

in an independent state from which field holder.Items

is currently contained. How do you achieve this?

+3


source to share





All Articles