In EF6 works this code:
public string GetConnectionString(DbContext ctx) { ObjectContext _objectContext = ((IObjectContextAdapter)ctx).ObjectContext; if (_objectContext?.Connection != null) { EntityConnection entityConnection = _objectContext.Connection as EntityConnection; return entityConnection?.StoreConnection?.ConnectionString; } return null; }
How to do it in EF Core 2.0 ?
The providerName setting is not required on EF Core connection strings stored in App. config because the database provider is configured via code. You can then read the connection string using the ConfigurationManager API in your context's OnConfiguring method. You may need to add a reference to the System.
The ASP.NET Core configuration system is especially stretchy in which the connection string is stored in the appsettigs. json, the user stores secret, an environment variable, and the configuration source.
Once the breakpoint is hit hover over query to expand then click on > Debug View > The dropdown beside the magnifying glass> Text Visualizer. There you have it, the SQL that will be sent to the database. Text Visualizer option showing the query generated by EF Core 5.
var connectionString = ctx.Database.GetDbConnection().ConnectionString;
with EF Core 5:
var connectionString = ctx.Database.GetConnectionString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With