title: Best Practice to use connection string in Asp.Net Core app
body: <p>I can't use EF because it doesn't work with Oracle (or the tutorials I have found are outdated and not usable).</p>
  
  <p>I am planning on using ODP.Net or devart's driver.</p>
  
  <p>What is the best way to use a connection string from appsettings.json?</p>
  
  <p>Ive seen so many tutorials that are incomplete or don't work with Asp.Net Core 3.1</p>
  
  <p>My controller</p>
  
  <pre><code>public class ReportController : Controller
  {
      private readonly ConnectionString myString;
      public ReportController(IOptions&lt;ConnectionString&gt; connectionString)
      {
          myString = connectionString.Value;
      }
  
      public ActionResult Reporting_Totals()
      {
          string connectionString = myString.ToString();
          DataTable dt = new DataTable();
          using (OracleConnection oracleConnection = new OracleConnection())
          return View();
      }
  </code></pre>
  
  <p>ConnectionString class</p>
  
  <pre><code>public class ConnectionString
  {        
      private string connectionString { get; set; }
      // enable set in singleton property instance to work correctly.
      //public static ConnectionString Instance { get; set; } = new ConnectionString();
  
      //public string DatabaseConnection { get; set; }
  }
  </code></pre>
  
  <p>My Startup class</p>
  
  <pre><code>    public Startup(IConfiguration configuration)
      {
          Configuration = configuration;
          //configuration.GetSection("ConnectionString").Bind(ConnectionString.Instance);
      }
  
      public IConfiguration Configuration { get; }
  
      // This method gets called by the runtime. Use this method to add services to the container.
      public void ConfigureServices(IServiceCollection services)
      {
          services.Configure&lt;ConnectionString&gt;(Configuration.GetSection("ConnectionStrings"));
          services.AddControllersWithViews();
      }
  </code></pre>
  
  <p>My appsettings.json</p>
  
  <pre><code>  "ConnectionStrings": {
      "DEV": "Server=myServer;Database=myDb1;Trusted_Connection=True;",
      "PROD": "Server=myServer;Database=myDb2;Trusted_Connection=True;"
  </code></pre>