[英]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. 更新 :问题是我正在评估在构造函数中使用哪种执行策略,而不是将其留在lambda中。 In other words, I had this inside the constructor of MyConfiguration...
换句话说,我在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? 原始问题:在Entity Framework 6.0中,是否有任何方法可以使DbContext上的某些查询使用一种执行策略,而其他查询则使用另一种执行策略? I see the below example in Microsoft's documentation .
我在Microsoft文档中看到以下示例。
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. 不幸的是,在我的测试中,如果我创建了DbContext类的多个实例,则看起来只有一个对DbConfiguration构造函数的调用,并且该单例配置对象在上下文对象之间共享。 So while I could set this at startup with SuspendExecutionStrategy , I couldn't change it at runtime.
因此,尽管我可以在启动时使用SuspendExecutionStrategy进行设置 ,但是我无法在运行时进行更改。 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).
我想象以后对SetExecutionStrategy ()的调用可能会更改它,但是我有多个线程,并且希望某些线程使用一个DbExecutionStrategy,而另一些线程使用另一个线程 (特别是针对可重试和不可重试的查询)。
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 ? 是否有某些设置可以将DbConfiguration更改为不是该类的单例,而是特定于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. 问题是我正在评估在构造函数中使用哪种执行策略,而不是将其留在lambda中。 In other words, I had this inside the constructor of MyConfiguration...
换句话说,我在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).
这是一个细微的差异,但是在实际评估时会有所变化(在施工中还是在使用中)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.