0

I'm starting a new project with a friend, I will be using windows and he works on macOS.

The project uses EntityFrameworkCore and I configure the connection string as this (inside the context class):

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if (!optionsBuilder.IsConfigured)
        optionsBuilder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));

    base.OnConfiguring(optionsBuilder);
}

configuration is an injected service of type IConfiguration.

In windows this works perfectly but mac fails saying that it can't use "null" as value. GetConnectionString is indeed returning null for mac.

I found many places saying that I need to use UseContentRoot in the program.cs:

public static void Main(string[] args)
{
    WebHost.CreateDefaultBuilder(args)
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build()
        .Run();
}

But it doesn't work on mac anyway.

Also, in macOS Directory.GetCurrentDirectory() will return <ProjectRoot>/bin/debug/net462 and windows will return <ProjectRoot>.

So I assume that it doesn't find appsettings.json inside that path and therefore it fails.

I also checked if I could access wwwroot and I can't. It's always using the bin instead of project root.

Any idea how to fix this?

Thank you.

PS

Different from the question tagged as "duplicate from":

There he isn't using UseContentRoot and neither IConfiguration to retrieve a > connectionString. I've seen that and it doesn't work

Leandro Soares
  • 2,902
  • 2
  • 27
  • 39
  • 1
    Possible duplicate of [How to get root directory of project in asp.net core. Directory.GetCurrentDirectory() doesn't seem to work correctly on a mac](https://stackoverflow.com/questions/43709657/how-to-get-root-directory-of-project-in-asp-net-core-directory-getcurrentdirect) – Kirk Larkin Dec 06 '18 at 14:51
  • 1
    @KirkLarkin, it's different. There he isn't using `UseContentRoot` and neither IConfiguration to retrieve a `connectionString`. I've seen that and it doesn't work. – Leandro Soares Dec 06 '18 at 15:28

0 Answers0