Set database timeout in Entity Framework

后端 未结 9 1767
囚心锁ツ
囚心锁ツ 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:24

    My partial context looks like:

    public partial class MyContext : DbContext
    {
        public MyContext (string ConnectionString)
            : base(ConnectionString)
        {
            this.SetCommandTimeOut(300);
        }
    
        public void SetCommandTimeOut(int Timeout)
        {
            var objectContext = (this as IObjectContextAdapter).ObjectContext;
            objectContext.CommandTimeout = Timeout;
        }
    }
    

    I left SetCommandTimeOut public so only the routines I need to take a long time (more than 5 minutes) I modify instead of a global timeout.

    0 讨论(0)
  • 2020-11-29 17:28

    I extended Ronnie's answer with a fluent implementation so you can use it like so:

    dm.Context.SetCommandTimeout(120).Database.SqlQuery...

    public static class EF
    {
        public static DbContext SetCommandTimeout(this DbContext db, TimeSpan? timeout)
        {
            ((IObjectContextAdapter)db).ObjectContext.CommandTimeout = timeout.HasValue ? (int?) timeout.Value.TotalSeconds : null;
    
            return db;
        }
    
        public static DbContext SetCommandTimeout(this DbContext db, int seconds)
        {
            return db.SetCommandTimeout(TimeSpan.FromSeconds(seconds));
        } 
    }
    
    0 讨论(0)
  • 2020-11-29 17:29

    You can use this simple :
    dbContext.Database.SetCommandTimeout(300);

    0 讨论(0)
  • 2020-11-29 17:31

    Same as other answers, but as an extension method:

    static class Extensions
    {
        public static void SetCommandTimeout(this IObjectContextAdapter db, TimeSpan? timeout)
        {
            db.ObjectContext.CommandTimeout = timeout.HasValue ? (int?) timeout.Value.TotalSeconds : null;
        }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-29 17:39

    You can use DbContext.Database.CommandTimeout = 180; // seconds

    It's pretty simple and no cast required.

    0 讨论(0)
提交回复
热议问题