Muds
Muds

Reputation: 4116

How can I set Entity Framework CommandTimeout in AutoGenerated Edmx file

We are using EF6 as DB First and the models and edmx files are generated using .tt templates.

Now we need to set command time out which is fairly straight forward

Partial Public Class ReservingEntities
Inherits DbContext

Public Sub New()
    MyBase.New("name=ReservingEntities")
    Database.CommandTimeout = 0
End Sub

But the problem is everytime we update our EF from database Database.CommandTimeout = 0 line is removed as class is constructed from template.

Can you suggest what is the right place to set the timeout so that it is persisted ?

Upvotes: 2

Views: 1133

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125292

You can use either of these options:

  • You can customize t4 templates of your current project.

  • You also can edit visual studio templates for entity framework. Then each edmx that you add to the project using Add New Item window, choosing ADO.NET Entity Data Model, will use modified templates.

Customize the entity framework templates for current projet

You can edit the .Context.tt template which is under .edmx file and add your code there.

In VB.NET those .tt files are hidden. To change them, in solution explorer toolbar click Show all files to see .tt files under your .edmx file. Then open .Context.tt file and add your code after this block:

Partial <#=Accessibility.ForType(container)#> Class <#=code.Escape(container)#>
    Inherits DbContext

    Public Sub New()
        MyBase.New("name=<#=container.Name#>")

In C#, files are not hidden and you can open .Context.tt file and put the code after this block:

<#=Accessibility.ForType(container)#> partial class <#=code.Escape(container)#> : DbContext
{
    public <#=code.Escape(container)#>()
        : base("name=<#=container.Name#>")
    {

Customize the entity framework templates that Visual Studio use

You can customize Visual Studio item templates for entity framework. Then each edmx that you add to the project using Add New Item window, choosing ADO.NET Entity Data Model, will use modified templates.

For example, for C# language and locale 1033 and EF6, you can edit CSharpDbContext.Context.tt file which is stored at below path:

\VisualStudioInstallationPath\Common7\IDE\ItemTemplates\CSharp\Data\1033\DbCtxCSEF6

Upvotes: 3

Related Questions