1

I've watched microsoft video about entity framework 6.1.1 and have seen example with soft delete. I am using this approach, but how can I now, using Code first aproach, get those deleted rows?

(https://github.com/rowanmiller/Demo-TechEd2014)

kul_mi
  • 1,131
  • 5
  • 15
  • 33
  • I don't want to go watch the whole video right now. Does the technique use a discriminator to filter deleted items? Like the solution over here - http://stackoverflow.com/a/18985828/150342 – Colin Sep 05 '14 at 11:09
  • It is not the video that I linked, but a github containing code. Here's ink to interceptor that is used - https://github.com/rowanmiller/Demo-TechEd2014/blob/master/FakeEstate.ListingManager/Models/EFHelpers/SoftDeleteInterceptor.cs – kul_mi Sep 05 '14 at 11:20

1 Answers1

0

I don't know if this fits with your implementation of soft delete, but it may be a fallback that you can use. I use this soft delete for entity framework technique. It means that if I need to access a soft deleted record I have to write my own sql query. I do it like this:

The DbSet has a SqlQuery method that creates a raw SQL query that will return entities.

I have this method in my generic repository:

   protected IEnumerable<T> SqlQuery(string sql, params object[] parameters)
    {
        if (String.IsNullOrEmpty(sql))
            throw new ArgumentException("sql is null or empty.", "sql");

        DbSqlQuery<T> q = Context.Set<T>().SqlQuery(sql, parameters);
        return q;
    }

I can call it from a repository that inherits my generic repository like this:

    public override void Delete(int id)
    {
        var files = SqlQuery(@"SELECT * FROM dbo.PropertyFiles WHERE 
            IsDeleted = 1 AND DATEADD(MINUTE, 30, DeletedAt) < GETUTCDATE()");
        foreach (PropertyFile file in files)
        {
            try
            {
                File.Delete(file.FilePath);

            }
            catch (Exception ex)
            {
                //TODO log the errors encountered when attempting to delete
            }
        }
        base.Delete(id);
    }
Community
  • 1
  • 1
Colin
  • 22,328
  • 17
  • 103
  • 197