Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get connectionString from EF Core 2.0 DbContext

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 ?

like image 840
Cyrus Avatar asked Jul 26 '17 09:07

Cyrus


People also ask

How do I get the EF core connection string?

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.

Where does NET Core store connection string?

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.

How do I find my EF core query?

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.


1 Answers

var connectionString = ctx.Database.GetDbConnection().ConnectionString; 

with EF Core 5:

   var connectionString = ctx.Database.GetConnectionString(); 
like image 130
ErikEJ Avatar answered Sep 28 '22 19:09

ErikEJ