How to recreate Execution Timeout Expired in EF6?

Viewed 37

I want to recreate one error scenario from application hosted on Azure.

While updating entities ( while importing some files from excel ) there is an error throwed :

System.Data.SqlClient.SqlException: Execution Timeout Expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.

error occurs when SaveChanges() is called

I think that inserting data is too large and that caused an issue. Due to that I need to recreate it locally. For that, I want to decrease this timeout but I can't do it. What I already tried to do:

First approach:

public AppDbContext() : base("AppDb")
        {
            _logger = LogManager.GetCurrentClassLogger();
            Database.CommandTimeout = 10; // normlany this is set to 180 but I want to avoid 
                                             large time for waiting for error so I set it to 
                                             10
        }

Second approach:

I changed my connection string

Data Source=.\SQLEXPRESS;Initial Catalog=Application-DEV;Integrated Security=True;MultipleActiveResultSets=true;Connection Timeout=1

I also tried to make it this way:

using (var context = new AppDbContext())
            {
                context.Database.CommandTimeout = 2;
                var breakPointOne = 1;

                context.FunctionalLocations.AddRange(data);
                context.SaveChanges();

                var breakPointTwo = 2;  // this breakpoint is reached after 50 seconds... 
            }
    // why exception isn't throwed?

anyone can tell me how to decrease this time? maybe I need to do something in db?

1 Answers

Not sure if I understand the scenario correctly but have you tried setting the CommandTimeout() options in the OnConfiguring method of DBContext. Something like this(code for EF Core)

    protected async override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var dbConn = configuration["DB:Connection"];

            optionsBuilder.UseSqlServer(dbConn,
              x => x.CommandTimeout(10).EnableRetryOnFailure());
        }
Related