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 typeIConfiguration
.
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