Hi,
I have an apllication that load a table in a list with dbcontext class.
The problem is the connection timeout, this is the error:
Microsoft.Data.SqlClient.SqlException: 'Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.'
The view that load data in sql server have an elapsed time of 9 minutes so how can i increase the timeout in ef core 6?
Thanks to everyone.

Amit MohantyRank #112 · 20K reputationPosted Jun 15, 2023, 7:36 AM EDT
You can increase the timeout for executing queries by configuring the CommandTimeout property of the DbContext class:
public class YourDbContext : DbContext { public YourDbContext(DbContextOptions options) : base(options) { // Increase the command timeout to a desired value (in seconds) Database.SetCommandTimeout(600); // Example: 10 minutes (600 seconds) } // ... your DbSet properties and other code ... }Or you can set the CommandTimeout property of the DbContext.Database property:
using (var context = new YourDbContext()) { // Set the timeout value (in seconds) context.Database.SetCommandTimeout(600); // 10 minutes // Perform your database operation that is causing the timeout // For example, if you're querying a table: var result = context.YourTable.ToList(); // Continue with further processing }Rajkiran SwainRank #165 · 14.4K reputationPosted Jun 15, 2023, 7:11 AM EDT
using Microsoft.EntityFrameworkCore; // ... // Create an instance of DbContextOptionsBuilder and specify your connection string var optionsBuilder = new DbContextOptionsBuilder(); optionsBuilder.UseSqlServer("your_connection_string"); // Set the command timeout value in seconds optionsBuilder.CommandTimeout = 600; // 10 minutes // Create an instance of your DbContext using the configured options using (var context = new YourDbContext(optionsBuilder.Options)) { // Your code to load data from the table using the DbContext }Tuhin PaulRank #7 · 71.9K reputationPosted Jun 15, 2023, 10:19 PM EDT
public class YourDbContext : DbContext { public YourDbContext(DbContextOptions options) : base(options) { // Increase the command timeout to a desired value (in seconds) Database.SetCommandTimeout(600); // Example: 10 minutes (600 seconds) } // ... your DbSet properties and other code ... }In this code, the YourDbContext class inherits from DbContext and sets the command timeout value in its constructor using the Database.SetCommandTimeout method. You can adjust the timeout value (in seconds) to your desired duration. If you're not using dependency injection or need to set the timeout for a specific database operation, you can use the CommandTimeout property of the DbContext.Database property:
using (var context = new YourDbContext()) { // Set the timeout value (in seconds) context.Database.SetCommandTimeout(600); // 10 minutes // Perform your database operation that is causing the timeout // For example, if you're querying a table: var result = context.YourTable.ToList(); // Continue with further processing }After creating an instance of YourDbContext, we set the CommandTimeout property of the Database property to the desired timeout value before executing the database operation.