将SetExecutionStrategy设置为SqlAzure

将SetExecutionStrategy设置为SqlAzure

本文介绍了使用DbMigrationsConfiguration将SetExecutionStrategy设置为SqlAzureExecutionStrategy吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天看到了一篇有关实现SqlAzureExecutionStrategy的帖子:

I saw a post today about implementing SqlAzureExecutionStrategy:

http://romiller.com/tag/sqlazureexecutionstrategy/

但是,我可以找到的所有示例都使用从DbConfiguration继承的Configuration.我的项目使用的是EF6代码优先迁移,它创建的配置继承自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将SetExecutionStrategy设置为SqlAzureExecutionStrategy吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 00:51