I have an application which is currently using DbContextPooling. I have been tasked with rotating primary and secondary connection strings. So is there a way of handling this in EF Core?
Sure. It's even easy. It may be incompatible with DbContextPooling, however. You'll need to test.
On your DbContext implement OnConfiguring, acquire the correct connection string and use it.
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var connectionString = ...;
optionsBuilder.UseSqlServer(connectionString);
base.OnConfiguring(optionsBuilder);
}
-
Yeah, with connection pooling, you'll end up with a mixed bag, with some connections to one and some connections to another and an almost random selection of which connection is actually used. This is actually kind of dangerous even without pooling, as there could be outstanding connections to the "bad" instance that will suddenly fail when that goes down. – Chris Pratt Jun 14 '19 at 20:22
-
"Connection Pooling" is fine. It's only DbContextPooling that's an issue. IE your Sql Server connections will be pooled and reused across DbContext instances even without DbContextPooling. – David Browne - Microsoft Jun 14 '19 at 20:45
-
-
-
@JohnMorsley Sql Client Connection Pooling: docs.microsoft.com/en-us/dotnet/framework/data/adonet/… – David Browne - Microsoft Jun 14 '19 at 21:20
primary
andsecondary
mean though and why would you rotate them? If you ask about failover, it doesn't work that way – Panagiotis Kanavos Jun 14 '19 at 16:05