Entity Framework Timeouts
There is a known bug with specifying default command timeout within the EF connection string.
http://bugs.mysql.com/bug.php?id=56806
Remove the value from the connection string and set it on the data context object itself. This will work if you remove the conflicting value from the connection string.
Entity Framework Core 1.0:
this.context.Database.SetCommandTimeout(180);
Entity Framework 6:
this.context.Database.CommandTimeout = 180;
Entity Framework 5:
((IObjectContextAdapter)this.context).ObjectContext.CommandTimeout = 180;
Entity Framework 4 and below:
this.context.CommandTimeout = 180;
Set database timeout in Entity Framework
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
How can I avoid TimeOut exception using Entity Framework with a stored procedure?
Entity Framework uses underlying connection provider and depends on the timeout provided by the provider. Usually its 30 seconds for the timeout if I am not mistaken.
However you can always increase the timeout by setting value of context.CommandTimeout = 120
in seconds
Hope this helps.
How to set command timeout in aspnetcore/entityframeworkcore
If you're using the DI container to manage the DbContext (i.e. you're adding the DbContext to the service collection), the command timeout can be specified in the options.
In Startup.ConfigureServices:
services.AddDbContext<YourDbContext>(options => options.UseSqlServer(
this.Configuration.GetConnectionString("YourConnectionString"),
sqlServerOptions => sqlServerOptions.CommandTimeout(60))
);
Set Command Timeout in EF 6
Check this :
Entity Framework 6 :
this.context.Database.CommandTimeout = 180;
Entity Framework 5:
((IObjectContextAdapter)this.context).ObjectContext.CommandTimeout = 180;
Entity Framework 4 and below:
this.context.CommandTimeout = 180;
EF Core 5 Upgrade - Query Timeouts
Consider using Split Queries to improve performance on queries with lots of Include's.
Related Topics
Where Is the Application.Doevents() in Wpf
Add New Item in Existing Array in C#.Net
How to Change Menu Hover Color
How to Serialize a Dictionary as Part of Its Parent Object Using JSON.Net
Order of Linq Extension Methods Does Not Affect Performance
Passing Values Between Windows Forms C#
Reading PDF Content Using Itextsharp in C#
Best Practices for Exception Management in Java or C#
Getting Value from Appsettings.JSON in .Net Core
List of Timezone Ids for Use with Findtimezonebyid() in C#
Differencebetween Task.Run() and Task.Factory.Startnew()
Entity Framework 4 - Addobject VS Attach
Can Anyone Explain Ienumerable and Ienumerator to Me
Call One Constructor from Another
Passing Properties by Reference in C#
How to Safely Call an Async Method in C# Without Await
Prevent Caching in ASP.NET MVC for Specific Actions Using an Attribute