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.