简体   繁体   中英

Are Query-Specific DbExecutionStrategies possible in EF6?

Update : The issue was I was evaluating which execution strategy to use in the constructor rather than leaving it in the lambda. In other words, I had this inside the constructor of MyConfiguration...

var strategy = SuspendExecutionStrategy 
    ? (IDbExecutionStrategy)new DefaultExecutionStrategy()
    : new SqlAzureExecutionStrategy());
this.SetExecutionStrategy("System.Data.SqlClient", () => strategy);

instead of having that evaluation inside the lambda.

Original Question: Is there any way in Entity Framework 6.0 to have some queries on a DbContext use one execution strategy and others use a different one? I see the below example in Microsoft's documentation .

public class MyConfiguration : DbConfiguration
{
    public MyConfiguration()
    {
        this.SetExecutionStrategy("System.Data.SqlClient", () => SuspendExecutionStrategy
          ? (IDbExecutionStrategy)new DefaultExecutionStrategy()
          : new SqlAzureExecutionStrategy());
    }

    public static bool SuspendExecutionStrategy
    {
        get
        {
            return (bool?)CallContext.LogicalGetData("SuspendExecutionStrategy")  false;
        }
        set
        {
            CallContext.LogicalSetData("SuspendExecutionStrategy", value);
        }
    }
}

Unfortunately, in my testing, it looks like if I create multiple instances of my DbContext class, there's only a single call to DbConfiguration's constructor with that singleton configuration object getting shared between the context objects. So while I could set this at startup with SuspendExecutionStrategy , I couldn't change it at runtime. I imagine later calls to SetExecutionStrategy () could change it, but I have multiple threads and would like some to be using one DbExecutionStrategy and others to be using a different one (specifically for retryable vs non-retryable queries).

Is there some setting somewhere that could change DbConfiguration to not be a singleton for the class and instead be specific to a particular instance of DbContext ? Or am I misunderstanding something entirely?

Ideally, I'd love to be able to do something like...

using(var repo = new MyDbContext()){
    repo.SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy());
    // do some queries to my repository
}

in one thread and then in another, have

using(var repo = new MyDbContext()){
    repo.SetExecutionStrategy("System.Data.SqlClient", () => (IDbExecutionStrategy)new DefaultExecutionStrategy());
    // do some queries to my repository
}

The issue was I was evaluating which execution strategy to use in the constructor rather than leaving it in the lambda. In other words, I had this inside the constructor of MyConfiguration...

var strategy = SuspendExecutionStrategy 
    ? (IDbExecutionStrategy)new DefaultExecutionStrategy()
    : new SqlAzureExecutionStrategy());
this.SetExecutionStrategy("System.Data.SqlClient", () => strategy);

instead of having that evaluation inside the lambda. It's a subtle difference, but it changes when it's actually evaluated (on construction vs on-use).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM