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
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
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-generatedDbContext
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 aspartial
, you can safely create a separate file with your customDbContext
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 yourDbConfiguration
class is namedDataContextConfiguration
, theentityFramework
configuration section in yourWeb.config
orApp.config
file should look like this:Just ensure that the namespace of your
DataContextConfiguration
class matches the namespace specified in thecodeConfigurationType
attribute. This will direct Entity Framework to use theDataContextConfiguration
class for the configuration.Remember to add the
DbConfigurationType
attribute on yourDbContext
class in a separate fileBy keeping the customizations in a separate file, you can ensure that your configuration remains intact even when the EDMX-based classes are regenerated.
References:
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.
Code: