SetExecutionStrategy to SqlAzureExecutionStrategy with DbMigrationsConfiguration?
P

2

19

I saw a post today about implementing SqlAzureExecutionStrategy:

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

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.

Can this be done?

Pinebrook answered 26/9, 2014 at 17:0 Comment(0)
P
40

If anyone else comes across this question, this is what we figured out:

Create a custom class that inherits from DbConfiguration (which has SetExecutionStrategy):

public class DataContextConfiguration : DbConfiguration
{
    public DataContextConfiguration()
    {
        SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy());
    }
}

Then add this attribute to your DataContext, specifying that it is to use your custom class:

[DbConfigurationType(typeof(DataContextConfiguration))]
public class DataContext : DbContext, IDataContext
{
    ...
}
Pinebrook answered 1/10, 2014 at 11:38 Comment(2)
What I found is that both kinds of configuration are found., my configuration class is being found and the constructor executed, regardless of the above problem. In other words, it worked for me.Clermontferrand
@Richardissimo thanks for the reminder. I've gone back through my posts and accepted some relevant answers.Pinebrook
K
3

After more investigation, now I think the correct answer is that: DbMigrationsConfiguration is completely separate and only configures the migration settings. That's why it doesn't inherit from or have the same options as DbConfiguration.

It is not loaded, and is irrelevant, for actual operation.

So you can (and should) declare a separate class based on DbConfiguration to configure the runtime behaviour.

I added some tracing and I saw that the first time you use a DatabaseContext in an application, it runs up the migration, and the migration configuration. But, the first time the DatabaseContext is actually used (e.g. to load some data from the database) it will load your DbConfiguration class as well.

So I don't think there is any problem at all.

Kingcup answered 9/10, 2015 at 2:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.