Set database timeout in Entity Framework

后端 未结 9 1775
囚心锁ツ
囚心锁ツ 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.

提交回复
热议问题