Set database timeout in Entity Framework

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

    In the generated constructor code it should call OnContextCreated()

    I added this partial class to solve the problem:

    partial class MyContext: ObjectContext
    {
        partial void OnContextCreated()
        {
            this.CommandTimeout = 300;
        }
    }
    
    0 讨论(0)
  • 2020-11-29 17:47

    I just ran in to this problem and resolved it by updating my application configuration file. For the connection in question, specify "Connection Timeout=60" (I am using entity framework version 5.0.0.0)

    ConnectionTimeout Setting

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

    Try this on your context:

    public class MyDatabase : DbContext
    {
        public MyDatabase ()
            : base(ContextHelper.CreateConnection("Connection string"), true)
        {
            ((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 180; // seconds
        }
    }
    

    If you want to define the timeout in the connection string, use the Connection Timeout parameter like in the following connection string:

    <connectionStrings>
    
    <add name="AdventureWorksEntities"
    connectionString="metadata=.\AdventureWorks.csdl|.\AdventureWorks.ssdl|.\AdventureWorks.msl;
    provider=System.Data.SqlClient;provider connection string='Data Source=localhost;
    Initial Catalog=AdventureWorks;Integrated Security=True;Connection Timeout=60;
    multipleactiveresultsets=true'" providerName="System.Data.EntityClient" />
    
    </connectionStrings>
    

    Source: How to: Define the Connection String

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