Entity Framework 6 Disable Interception temporarily

database entity-framework entity-framework-6 soft-delete

Accepted Answer

My database context class has been expanded with new properties.

[DbConfigurationType(typeof(DbConfig))] 
public partial class YourEntitiesDB 
{
    public bool IgnoreSoftDelete { get; set; }
}

I then check this flag in the TreeCreated(...) function, and if it is true, it just skips the QueryVisitor step.

public class SoftDeleteInterceptor : IDbCommandTreeInterceptor
{
    public SoftDeleteInterceptor()
    {

    }
    public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext)
    {
        var db = interceptionContext.DbContexts.FirstOrDefault() as YourEntitiesDB;
        if (db!=null && db.IgnoreSoftDelete)
        {
            // Ignore soft delete interseptor (Used in archives)
            return;
        }

        if (interceptionContext.OriginalResult.DataSpace == DataSpace.CSpace)
        {
            var queryCommand = interceptionContext.Result as DbQueryCommandTree;
            if (queryCommand != null)
            {
                var newQuery = queryCommand.Query.Accept(new SoftDeleteQueryVisitor());
                interceptionContext.Result = new DbQueryCommandTree(
                    queryCommand.MetadataWorkspace,
                    queryCommand.DataSpace,
                    newQuery);
            }

            var deleteCommand = interceptionContext.OriginalResult as DbDeleteCommandTree;
            if (deleteCommand != null)
            {
                var column = SoftDeleteAttribute.GetSoftDeleteColumnName(deleteCommand.Target.VariableType.EdmType);
                if (column != null)
                {
                    var setClauses = new List<DbModificationClause>();
                    var table = (EntityType)deleteCommand.Target.VariableType.EdmType;
                    if (table.Properties.Any(p => p.Name == column))
                    {
                        setClauses.Add(DbExpressionBuilder.SetClause(
                                DbExpressionBuilder.Property(
                                    DbExpressionBuilder.Variable(deleteCommand.Target.VariableType, deleteCommand.Target.VariableName),
                                    column),
                                DbExpression.FromBoolean(true)));
                    }

                    var update = new DbUpdateCommandTree(
                        deleteCommand.MetadataWorkspace,
                        deleteCommand.DataSpace,
                        deleteCommand.Target,
                        deleteCommand.Predicate,
                        setClauses.AsReadOnly(),
                        null);

                    interceptionContext.Result = update;
                }
            }
        }
    }
}

I just set the flag to true as necessary in order to utilise it.

YuorEntitiesDB DB = new YuorEntitiesDB();
DB.IgnoreSoftDelete = true;
DB.Records.Where(...)
8
10/18/2017 11:37:34 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow