skip to Main Content

API Project -> C# -> .Net 4.8 -> EF6 -> Moved over to an Azure SQL DB and getting hit with this on occasion:

An exception has been raised that is likely due to a transient failure. If you are connecting to a SQL Azure database consider using SqlAzureExecutionStrategy

I used the accepted answer here to help me answer my question:
SetExecutionStrategy to SqlAzureExecutionStrategy with DbMigrationsConfiguration?

and it seems to have worked.

I created a new class:

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

and added this as a reference to my context.cs file:

[DbConfigurationType(typeof(DataContextConfiguration))]
public partial class APIEntities : DbContext
{

and all seems to be working but it states at the top of the APIEntities file:

<auto-generated>
This code was generated from a template.
Manual changes to this file may cause unexpected behavior in your application.
Manual changes to this file will be overwritten if the code is regenerated.
</auto-generated>

Meaning this will re-generated when a change is made to the edmx file.

So looking at this post:
https://learn.microsoft.com/en-us/ef/ef6/fundamentals/configuring/code-based
enter image description here

I want to change this DataContextConfiguration to be loaded from the entityFrameworkcode ConfigurationType in the connection string but I’m having a slight confusion with the naming.

It says:

The value of codeConfigurationType must be the assembly and namespace qualified name of your DbConfiguration class.

So if my project is:

ProjectName1
-> DataContextConfiguration (FileName)

and the ‘MyAssembly’ if got by right clicking on the project and vieiwng assembly name:
(which is the same as ProjectName1)

Does this mean I would set the codeConfigurationType tag as:

  <entityFramework codeConfigurationType="ProjectName1.DataContextConfiguration, ProjectName1">

Is this correct please?

2

Answers


  1. When you move to a SQL Azure database, it’s recommended to use the SqlAzureExecutionStrategy to handle transient failures. The configuration changes you’ve made are a step in the right direction. To address your concern about the auto-generated DbContext file, it’s correct that any manual changes to this file will be lost if the code is regenerated from the EDMX file. However, since EF classes are defined as partial, you can safely create a separate file with your custom DbContext class modifications.

    Now, to specify your DbConfiguration class in the config file, you are correct in your approach. If your project’s assembly name and default namespace are "ProjectName1" and your DbConfiguration class is named DataContextConfiguration, the entityFramework configuration section in your Web.config or App.config file should look like this:

    <entityFramework codeConfigurationType="ProjectName1.DataContextConfiguration, ProjectName1">
      <!-- Your EF config -->
    </entityFramework>
    

    Just ensure that the namespace of your DataContextConfiguration class matches the namespace specified in the codeConfigurationType attribute. This will direct Entity Framework to use the DataContextConfiguration class for the configuration.

    Remember to add the DbConfigurationType attribute on your DbContext class in a separate file

    namespace ProjectName1
    {
        [DbConfigurationType(typeof(DataContextConfiguration))]
        public partial class APIEntities : DbContext
        {
            // Your context's code here
        }
    }
    

    By keeping the customizations in a separate file, you can ensure that your configuration remains intact even when the EDMX-based classes are regenerated.

    References:

    Login or Signup to reply.
  2. I use the following to get this done. If the database name is Database1 then the following is the custom code I wrote to get this working.

    1. I created a public partial class for the existing auto-generated DbContext for my custom code.(This doesn’t get overwritten every time I run the template)
    2. I created the execution strategy and attached to this partial class.

    Code:

    [DbConfigurationType(typeof(CustomDbConfiguration))]
    public partial class Database1Entities : DbContext
    {
      public DBEntities(string pDbName) : base(pDbName)
      {}
    }
    
    
    
     public class CustomDbConfiguration : DbConfiguration
     {
         public CustomDbConfiguration()
         {
             SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy());
         }
     }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search