I should point out I’m new to .NET…
I have the following appsettings.json file:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings": {
"MyContext": "Host=localhost;Database=test;Username=user;Password=''"
}
}
In the MyContext.cs file I want to read the connection string from the appsettings.json config:
using Microsoft.EntityFrameworkCore;
using System;
using System.Configuration;
namespace My.Models
{
public partial class MyContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
Console.WriteLine(ConfigurationManager.ConnectionStrings.Count);
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseNpgsql(ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString);
}
}
}
But ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString
is null, any ideas why?
I’m using the config file successfully in Startup.cs via Configuration.GetConnectionString("MyContext")
, but here I want to read the connection string from this config for situations outside of the ASP.NET application (i.e. in EF migrations I want to do using var db = new MyContext();
), but have no idea why the above is returning null.
Is ConfigurationManager not reading from my appsettings.json file? Not sure how to tell, as ConfigurationManager.ConnectionStrings.Count
returns 1, so I’m pretty confused. Is there anything obviously wrong someone can see?
I’m using .NET Core 5 and EF Core version 5
2
Answers
the common way to define connection string in ef core is
You can access to IConfiguration manually:
if your Ef core is in a special project, you will have to copy appsetings.json to this project
if you have a problem to find appsetings folder you can define a path directly
I’m assuming that you’re running either an API or a Blazor/Razor application since you mentioned a
Startup.cs
file 🙂The recommendation (official docs about this), in this case, is to use Entity Framework’s own libraries to setup your
MyContext
withinStartup.ConfigureServices
. There you’ll have access to both theConnectionStrings
within your application’sIConfiguration
and to extension methods that allow you to register aDbContext
with a specific connection string.It’ll probably look something like this, just adapt from my call to
Sqlite
into whatever Database driver you’re using:This allows you to keep the Configuration logic from "leaking" into your "Model" namespace, thus your
MyContext
concern is just related to persistence of data and not accessing system files and environment variables.In order to use an Instance of
MyContext
on a Controller or anything else created by .NET’s dependency injection system all you need to do is ask for it within a class’ constructor, like: