Set database timeout in Entity Framework

后端 未结 9 1772
囚心锁ツ
囚心锁ツ 2020-11-29 16:42

My command keeps timing out, so I need to change the default command timeout value.

I\'ve found myDb.Database.Connection.ConnectionTimeout, but it\'s

9条回答
  •  [愿得一人]
    2020-11-29 17:38

    For Database first Aproach:

    We can still set it in a constructor, by override the ContextName.Context.tt T4 Template this way:

    <#=Accessibility.ForType(container)#> partial class <#=code.Escape(container)#> : DbContext
    {
        public <#=code.Escape(container)#>()
            : base("name=<#=container.Name#>")
        {
            Database.CommandTimeout = 180;
    <#
    if (!loader.IsLazyLoadingEnabled(container))
    {
    #>
            this.Configuration.LazyLoadingEnabled = false;
    <#
    }
    

    Database.CommandTimeout = 180; is the acutaly change.

    The generated output is this:

    public ContextName() : base("name=ContextName")
    {
        Database.CommandTimeout = 180;
    }
    

    If you change your Database Model, this template stays, but the actualy class will be updated.

提交回复
热议问题