1

In project we have connection timeout error in save changes in one method which updates a lot of data to Db. We use EF Core 6 and .NET6 and SqlServer.

To get this error I need to wait a lot of time, so I decided to decrease time of waiting and mock connection timeout time. But I can't achive it. I want something like this :

[HttpGet]
        public IActionResult SeedDb()
        {
            var persons = new Faker<Person>()
                .RuleFor(x => x.Name, y => y.Name.FirstName())
                .RuleFor(x => x.Surname, y => y.Name.LastName());

            var personsToDb = persons.Generate(250000);
            
            _context.AddRange(personsToDb);
            _context.SaveChanges(); // here I need to have connection timeout error

            return Ok();
        }

Above operation takes about 10 seconds. What I tried :

// directly in controller
_context.Database.SetCommandTimeout(1); 

// in DI configuration
options.UseSqlServer("Data Source=.\\SQLEXPRESS;Initial Catalog=TESTOWISKO;Integrated Security=True;MultipleActiveResultSets=true;Connection Timeout=60",
                    sqlServerOptions => sqlServerOptions.CommandTimeout(5)); 

// in connection string set below to 1
Connection Timeout=1

// in db context constructor

this.Database.SetCommandTimeout(1);

even of that I can't recreate this error, someone can tell me why above approaches not works and how to achieve that?

I will add , I saw this: Set database timeout in Entity Framework

post but there is no answer over there, I tried to use this solutions but that's not working. Next time when someone will close question , please make a better review.

ruddnisrus
  • 187
  • 5
  • Above article doesn't include answer for Your question as You said. Can You please tell if You found answer for that? I have similar problem – Mateusz Kaleta Nov 18 '22 at 16:36
  • 1
    @Mateusz Kaleta this question has been closed for some reasons... no I didn't found any answer for that, maybe if this one who closed it will review it again he will unblock and we will found answer – ruddnisrus Nov 18 '22 at 16:38
  • hopefully, I have similar problem and there is no working solution in post that You paste into post – Mateusz Kaleta Nov 18 '22 at 16:40
  • EF performs the insertion in small portions - batches. Each batch is inserted in a short time, not exceeding CommandTimeout. You can try increasing the [MaxBatchSize](https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.infrastructure.relationaloptionsextension.maxbatchsize?view=efcore-7.0) value. However, it cannot exceed 2100, which may not be enough to exceed the timeout. – Alexander Petrov Nov 18 '22 at 22:44
  • See Sql Server [limits](https://learn.microsoft.com/en-us/sql/sql-server/maximum-capacity-specifications-for-sql-server?redirectedfrom=MSDN&view=sql-server-ver16). See [Batching](https://learn.microsoft.com/en-us/ef/core/performance/efficient-updating#batching) for more info. – Alexander Petrov Nov 18 '22 at 22:45
  • @MateuszKaleta - see my comment. / The question, of course, was closed in vain. I voted for reopen. – Alexander Petrov Nov 18 '22 at 22:47
  • I think your task can be solved with the help of [Interceptors](https://learn.microsoft.com/en-us/ef/core/logging-events-diagnostics/interceptors). Using the interceptor, you can add the [WAITFOR](https://learn.microsoft.com/en-us/sql/t-sql/language-elements/waitfor-transact-sql?view=sql-server-ver16) DELAY command to the sql-query. – Alexander Petrov Nov 18 '22 at 23:01

0 Answers0