有一个基于DbConfiguration的CustomDbConfiguration类:
internal class CustomDBConfiguration : DbConfiguration
构造函数设置执行策略,拦截器用于生成瞬态失败:
/// <summary>
/// Constructor.
/// </summary>
public CustomDBConfiguration()
{
// Add transient failure.
AddInterceptor(new TransientFailure()); // <-- Used to create a transient failure.
// Set the execution strategy.
SetExecutionStrategy
(SqlProviderServices.ProviderInvariantName,
() => new MyDBExecutionStrategy(3, TimeSpan.FromSeconds(10)));
}
MyDBExecutionStrategy基于DbExecutionStrategy:
internal class MyDBExecutionStrategy : DbExecutionStrategy
发生错误时实现ShouldRetryOn布尔值:
/// <summary>
/// Retry on error?
/// </summary>
/// <param name="ex"></param>
/// <returns></returns>
protected override bool ShouldRetryOn(Exception ex)
但是,当我实例化我的DbContext时,如果我没有包含&#34; _context.Database.CommandTimeout = 1;&#34;然后不使用执行策略:
/// <summary>
/// Test by getting a list of people.
/// </summary>
private void TryItOut()
{
if (!string.IsNullOrEmpty(_connectionString))
{
try
{
// Create a new Context instance, with a connection string specified.
_context = new MyDBContext(_connectionString);
// --- IF I TEXT THIS LINE OUT THEN THE EXECUTION STRATEGY ISN'T IMPLEMENTED ---
_context.Database.CommandTimeout = 1;
List<Person> people = _context.People.ToList();
MessageBox.Show("People count: " + people.Count());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error occurred", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
在我看来,这条线似乎是多余的,但如果它不存在则不会调用该策略。
我做错了什么?
提前致谢。