1

We've been using EF6 for a while now to connect with an Azure database. For this database we use the ExecutionStrategy specific for Azure connection to have a more resilient connection:

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

See the microsoft article about connection resilience for more info

Recently however we've added a connection to a different database which resides on a MSSQL database server where we want to use the default execution strategy. Since this runs in the same app domain, we run into a problem:

The default DbConfiguration instance was used by the Entity Framework before the 'MyDbConfiguration' type was discovered. An instance of 'MyDbConfiguration' must be set at application start before using any Entity Framework features or must be registered in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information."

Reviewing the article linked in the error I see the following statement:

Create only one DbConfiguration class for your application. This class specifies app-domain wide settings.

I've tried solutions from several related questions to this, but I keep running in the same problem. The things I've tried basically come down to setting the custom DbConfiguration in different ways, through code, attribute or config file.

I think the solution is to set the execution strategy without a custom DbConfiguration, but I'm not really sure it is, and how I should do this.

Martijn
  • 422
  • 1
  • 6
  • 20

1 Answers1

-1

The default DbConfiguration instance was used by the Entity Framework before the 'MyDbConfiguration' type was discovered. An instance of 'MyDbConfiguration' must be set at application start before using any Entity Framework features or must be registered in the application's config file.

Place DbConfigurationTypeAttribute on your context class.

Change Context class as below:

[DbConfigurationType("MyNamespace.MyDbConfiguration, MyAssembly")] 
public class MyContextContext : DbContext
{ 
}

Create only one DbConfiguration class for your application. This class specifies app-domain wide settings.

Make sure that you have added sql data provider in config file :

Use the config file to specify the DbConfiguration instance.

To do this, set the codeConfigurationType attribute of the entityFramework section in (web.config).

<entityFramework codeConfigurationType="MyNamespace.MyDbConfiguration, MyAssembly">
    ...Your EF config...
</entityFramework>

Updated Answer

Either use the workarounds detailed here:
Entity Framework Limitations with Retrying Execution Strategies (EF6 onwards)

Or one of the techniques for overriding the DbConfiguration or using a different DbConfiguration here: Entity Framework Code-Based Configuration (EF6 onwards)

  • I have 3 questions about this: 1. This will still set one DbConfiguration for all DbContexts right? Meaning this will not solve my problem. 2. Why inherit from IdentityDbContext? This seems like something from ASP.NET which I am not using in this piece of code. 3. The documentation on microsoft in [this article](https://docs.microsoft.com/en-us/ef/ef6/fundamentals/configuring/code-based) states that config will have precedence over code based configuration. This will mean the attribute is redundant? – Martijn Oct 27 '21 at 11:37
  • Updated the answer. Please refer [Multiple DbContexts and DbConfigurations](https://stackoverflow.com/questions/20431184/ef6-dbconfigurationclass-was-set-but-this-type-was-not-discovered-multiple-d) and [SO](https://stackoverflow.com/questions/20308378/configure-multiple-database-entity-framework-6) – HarshithaVeeramalla-MT Oct 27 '21 at 11:50
  • I have seen the question you linked before asking this question and my problem is different because I need 2 different DbConfigurations. I did everything you've suggested and it breaks because the DbConfiguration which I need for the Azure database has an Execution Strategy which is not suitable for a regular Sql database. – Martijn Oct 27 '21 at 11:57
  • @Martijn - Please check the Updated answer – HarshithaVeeramalla-MT Oct 27 '21 at 12:17