Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add-migration can not read the connection string from the file appsettings.json because of the path to appsettings.json

Tools: VS2017, ASP.NET Core 2, Entity Framework Core 2, ASP.NET Core JavaScript Services

I am using the following BuildWebHost methode:

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
    .UseContentRoot(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName))
    .UseStartup<Startup>()
    .UseNLog()
    .Build();

For loading the connection string I have the following code in ConfigureServices (startup.cs):

Action<DbContextOptionsBuilder> optionsAction = options =>
    options.UseSqlServer(Configuration.GetConnectionString("RecipeDatabase")); 
services.AddDbContext<RecipeContext>(optionsAction);

With the above configuration the app runs without problems in debug mode and as windows service (after publishing).

But if I run add-migration the tool is not able to load the connection string from appsettings.json:

enter image description here

If I comment the following line like so

//.UseContentRoot(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName))

add-migration runs without problems but the app running as "Windows Service" not because it will not find the appsettings.json file.

How can I modify the configuration so that it is not necessary to comment the above line anymore?

Thank you.

Michael

like image 743
dergroncki Avatar asked Mar 25 '18 13:03

dergroncki


People also ask

How can I get the connection string from Appsettings json in .NET core API?

In order to add AppSettings. json file, right click on the Project in Solution Explorer. Then click Add, then New Item and then choose App Settings File option (shown below) and click Add button. Once the File is created, it will have a DefaultConnection, below that a new Connection String entry is added.

How do I add Appsettings to json?

Add Json File After adding the file, right click on appsettings. json and select properties. Then set “Copy to Ouptut Directory” option to Copy Always. Add few settings to json file, so that you can verify that those settings are loaded.


1 Answers

You might want to override OnConfiguring method in your DbContext. Here is an example I'm using. It will work whenever you are using command line or windows service:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if (optionsBuilder.IsConfigured)
    {
        base.OnConfiguring(optionsBuilder);
        return;
    }

    string pathToContentRoot = Directory.GetCurrentDirectory();
    string json = Path.Combine(pathToContentRoot, "appsettings.json");

    if (!File.Exists(json))
    {
        string pathToExe = Process.GetCurrentProcess().MainModule.FileName;
        pathToContentRoot = Path.GetDirectoryName(pathToExe);
    }

    IConfigurationBuilder configurationBuilder = new ConfigurationBuilder()
        .SetBasePath(pathToContentRoot)
        .AddJsonFile("appsettings.json");

    IConfiguration configuration = configurationBuilder.Build();

    optionsBuilder.UseSqlServer(configuration.GetConnectionString("RecipeDatabase"));

    base.OnConfiguring(optionsBuilder);
}
like image 129
Peska Avatar answered Sep 26 '22 01:09

Peska