问题描述
今天看到一篇关于实现 SqlAzureExecutionStrategy 的帖子:
I saw a post today about implementing SqlAzureExecutionStrategy:
http://romiller.com/tag/sqlazureexecutionstrategy/
但是,我能找到的所有示例都使用从 DbConfiguration 继承的配置.我的项目正在使用 EF6 Code First 迁移,它创建的配置继承自 DbMigrationsConfiguration.此类不包含 SetExecutionStrategy 的定义,并且我找不到将 SqlAzureExecutionStrategy(或任何 SetExecutionStrategy)与 DbMigrationsConfiguration 实际结合的示例.
However, all examples I can find of this use a Configuration that inherits from DbConfiguration. My project is using EF6 Code First Migrations, and the Configuration it created inherits from DbMigrationsConfiguration. This class doesn't contain a definition for SetExecutionStrategy, and I can find no examples that actually combine SqlAzureExecutionStrategy (or any SetExecutionStrategy) with DbMigrationsConfiguration.
这个可以吗?
推荐答案
如果其他人遇到这个问题,这就是我们想出的:
If anyone else comes across this question, this is what we figured out:
创建一个继承自 DbConfiguration(具有 SetExecutionStrategy)的自定义类:
Create a custom class that inherits from DbConfiguration (which has SetExecutionStrategy):
public class DataContextConfiguration : DbConfiguration
{
public DataContextConfiguration()
{
SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy());
}
}
然后将此属性添加到您的 DataContext 中,指定它要使用您的自定义类:
Then add this attribute to your DataContext, specifying that it is to use your custom class:
[DbConfigurationType(typeof(DataContextConfiguration))]
public class DataContext : DbContext, IDataContext
{
...
}
这篇关于使用 DbMigrationsConfiguration 将执行策略设置为 SqlAzureExecutionStrategy?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!