Entity Framework 6 Disable interception temporarily

I use IDbCommandTreeInterceptorto enable soft deletes in my model.

System.Data.Entity.Infrastructure.Interception.DbInterception.Add(
     new SoftDeleteInterception());

I want to temporarily disable the interceptor so that I can select the “remote” object for audit purposes.

However, it looks like the collection DbInterceptionis a team.

Is it possible to create a new one DbContextwithout interception? Or even a way to add an interceptor to DbContextevery time it is created?

+4
source share
1 answer

I extended my db context class with an additional property

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

Then, in the TreeCreated (...) method, I check this flag and, if true, it just doesn't go to QueryVisitor

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;
                }
            }
        }
    }
}

, true

YuorEntitiesDB DB = new YuorEntitiesDB();
DB.IgnoreSoftDelete = true;
DB.Records.Where(...)
+5

Source: https://habr.com/ru/post/1655652/


All Articles