3

Is it possible to disable an interceptor for different instances of DbContext with EF6 database first?

I apply interceptors the following way:

DbConfiguration.SetConfiguration(new InterceptorConfiguration());

InterceptorConfiguration is implemented like this:

public class InterceptorConfiguration : DbConfiguration
{
    public InterceptorConfiguration()
    {
        AddInterceptor(new SoftDeleteInterceptor<T>());
    }
} 

For the first time I implemented something very similar to this: Entity Framework 6 Disable Interception temporarily

That would work if I use DbContext locally in a desktop app. I am using WCF and if I am implement this way, it is only working for the first time for every query.

I extend the select with the condition DeleteDate is null. Next time I run the same select the interceptor is not running again. So when a query has run with soft-delete setting, the next time the interceptor will not run, this query is a soft-delete-setting query for the lifetime of the app, until I restart the WCF app.

I think that the reason is that DbExpressionBuilder hard-codes the contdition DeleteDate is null in the query string and EF expects that it will always be the query for that select statement and not generates again.

So I tried to put another condition: (DeleteDate is null AND 1=1). In palce of 1 I expected that EF will make parameters but it did not do that. It hardcoded this also.

The code of addign this fake true condition looks like this:

if (_softDelete)
{
    var notDeleted = DbExpressionBuilder.IsNull(property);
    int two = 2;
    int two2 = 2;
    var fakeTrue = DbExpressionBuilder.Equal(DbExpression.FromInt32(two), DbExpression.FromInt32(two2));
    var filter = DbExpressionBuilder.And(notDeleted, fakeTrue);
    DbExpressionBuilder.Filter(binding, filter);
}
else
{
    int two = 2;
    int two2 = 2;
    var fakeTrue = DbExpressionBuilder.Equal(DbExpression.FromInt32(two), DbExpression.FromInt32(two2));
    DbExpressionBuilder.Filter(binding, fakeTrue);
}

Maybe it would be good for me if I could make this DbExpressionBuilder use parameters, not hardcoding the values.

For Delete for example it is woking. I set the DeleteDate and EF makes it with parameters not hardcodes it in the query. So if my first Delete was running as soft-delete then the interceptor will run for the next Delete statement also.

dvjanm
  • 2,351
  • 1
  • 28
  • 42
  • Can you post a minimal working set of code to reproduce? E.g. a single simple Entity, db context, intercepor, and a test method – Michal Ciechan Jun 10 '17 at 09:03

0 Answers0