I have a EF Core 2 Dbcontext injected in the usual way into all Controllers of a Web API:
services.AddDbContext<KpiContext>(options =>
options.UseSqlServer(config.ConnectionStrings.KpiDB,
providerOptions => providerOptions.EnableRetryOnFailure()));
This is injected into controllers in the standard way as, say, kpidb
and then it's passed to a static method which does this:
somestaticmethod(KpiContext kpidb) {
// create something
kpidb.Add(something);
kpidb.SaveChanges();
}
This SaveChanges()
intermittently fails with:
System.Data.SqlClient.SqlException (0x80131904): A network-related or
instance-specific error occurred while establishing a connection to SQL
Server. The server was not found or was not accessible. Verify that the
instance name is correct and that SQL Server is configured to allow remote
connections. (provider: SQL Network Interfaces, error: 26 - Error Locating
Server/Instance Specified)
I can't see a pattern to this but it happens quite often, perhaps around 10% of the time. I've tried:
AddDbContextPool
instead of AddDbContext
Nothing seems to make a difference. I know that DBContext isn't thread safe but there are no async EF calls to fall foul of.
Any ideas appreciated.
User contributions licensed under CC BY-SA 3.0