Search code examples
c#entity-frameworkstaten-tier-architecture

Using N-Tier Entity Framework, how can I access an entity that has been marked as deleted?


Note: My project is using N-Tier Entity Framework (https://ntieref.codeplex.com/), which is different than regular Entity Framework.

On the Client side, when I attempt to access an entity that has been marked as deleted, it does not show up in my foreach loop.

foreach (var entity in DataContext.EntitySet.Where(x => x.ChangeTracker.State == ObjectState.Deleted))

I can’t access an entity that is marked as deleted.

var entity = DataContext.EntitySet.Where(x => x.ChangeTracker.State == ObjectState.Deleted).FirstOrDefault();

@ChristofSenn How do I access the entity marked as deleted when I’m in the Client, before saving changes?


Solution

  • Entities marked as deleted are hidden and don't get returned when enumerating the entity set. To get all entities (i.e. including deleted) you can use the following method:

    Interface:

    IEntitySet<TEntity>.GetAllEntities();
    

    Sample:

    foreach (var entity in DataContext.EntitySet
                                 .GetAllEntities()
                                 .Where(x => x.ChangeTracker.State == ObjectState.Deleted))