This is how I solved this problem when using the EDMX file. This solution modifies the default T4 template to make the generated class inherit from the custom DbContext class, which indicates the default command timeout and the property to change it.
I am using Visual Studio 2012 and EF 5.0. Your experience may vary in other versions.
Create your own DbContext class
public class CustomDbContext : DbContext { ObjectContext _objectContext; public CustomDbContext( string nameOrConnectionString ) : base( nameOrConnectionString ) { var adapter = (( IObjectContextAdapter) this); _objectContext = adapter.ObjectContext; if ( _objectContext == null ) { throw new Exception( "ObjectContext is null." ); } _objectContext.CommandTimeout = Settings.Default.DefaultCommandTimeoutSeconds; } public int? CommandTimeout { get { return _objectContext.CommandTimeout; } set { _objectContext.CommandTimeout = value; } } }
This has an additional function: I do not hardcode the default command timeout. Instead, I load it from the project settings to change the value in the configuration file. How to configure and use project settings is not included in the scope of this answer.
I also do not hardcode the connection string or the name of the connection string. He has already moved to the constructor with the generated context class, so there is no point in hard-coding it. This is nothing new; The EDMX file already generates the following constructor for you, so we just pass the value.
public MyEntities() : base("name=MyEntities") { }
(This instructs EF to load the configuration line named "MyEntities" from the configuration file.)
I throw a special exception if the ObjectContext always null. I don't think it will ever be, but it is more meaningful than getting a NullReferenceException .
I store an ObjectContext in a field so that I can make a property to access it to override the default value.
Changing the T4 Object Context Template
In Solution Explorer, expand the EDMX file so that you can see the T4 templates. They have the extension .tt.
Double-click the file "MyModel.Context.tt" to open it. On line 57 you should see this:
<#=Accessibility.ForType(container)#> partial class <#=code.Escape(container)#> : DbContext
This template line generates a class definition for your class "MyEntities", which inherits DbContext.
Modify the line so that the generated class inherits CustomDbContext, instead:
<#=Accessibility.ForType(container)#> partial class <#=code.Escape(container)#> : CustomDbContext
Once you save this file, it should restore the class. If not, you can right-click the EDMX file and select Run Custom Tool. If you expand the file "MyModel.Context.tt" under your EDMX file, you will see "MyModel.Context.cs". This is a generated file. Open it and you will see that it now inherits CustomDbContext .
public partial class MyEntities : CustomDbContext
That is all that is needed.
Questions
After changing the context class from DbContext to CustomDbContext , Visual Studio will give you an error message if you try to add a new MVC controller class using “Controller with read / write actions and views using the Entity Framework.” It will say “Unsupported context type” To work around this, open the generated class "MyModel.Context.cs" and temporarily change the type inherited to DbContext . After adding a new controller, you can change it to CustomDbContext .