问题
This is the structure of my project :
- App.UI
- App.Data (ClassLibrary project)
App.UI contains some environment variables that I would like App.Data to have access to such as the connection string.
The problem now is that when I try to do a migration like this:
Add-Migration AppOneBaseMigration -Context AppDbContext -OutputDir Migrations\AppOneMigrations
I get this error:
System.ArgumentNullException: Value cannot be null. Parameter name: path
This is the code that initialises when I run my migrations:
public AppDbContext CreateDbContext(string[] args)
{
//Debugger.Launch();
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(ConfigurationManager.GetBasePath(Environment.GetEnvironmentVariable("CENTRAL_APPLICATION_SETTINGS")))
.AddJsonFile("mssettings.json")
.Build();
var builder = new DbContextOptionsBuilder<AppDbContext>();
builder.UseSqlServer(configuration["DatabaseConfiguration:ConnectionString"]);
return new AppDbContext(builder.Options);
}
Is there another way I can pass the Environment.GetEnvironmentVariable("CENTRAL_APPLICATION_SETTINGS") variable to it?
I tried setting an environment variable in App.Data but it didn't pick it up.
回答1:
You should be able set the variable in the Package Manager Console before running the Add-Migration command by typing
$env:CENTRAL_APPLICATION_SETTINGS="your value"
And then run
Add-Migration AppOneBaseMigration -Context AppDbContext -OutputDir Migrations\AppOneMigrations
回答2:
- Environment variables are set in Windows.
Currently CENTRAL_APPLICATION_SETTINGS environment variable is not set and That's probably why you are getting exception.
- You cannot get the configuration values from the class library.
By default the configuration settings are loaded from the executable (the project which generates exe).
.Net core can load configuration values from various type of configurations (command line, XML/JSON files,etc). Below is sample from MSDN:
public class Program
{
public static Dictionary<string, string> arrayDict = new Dictionary<string, string>
{
{"array:entries:0", "value0"},
{"array:entries:1", "value1"},
{"array:entries:2", "value2"},
{"array:entries:4", "value4"},
{"array:entries:5", "value5"}
};
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.SetBasePath(Directory.GetCurrentDirectory());
config.AddInMemoryCollection(arrayDict);
config.AddJsonFile("json_array.json", optional: false, reloadOnChange: false);
config.AddJsonFile("starship.json", optional: false, reloadOnChange: false);
config.AddXmlFile("tvshow.xml", optional: false, reloadOnChange: false);
config.AddEFConfiguration(options => options.UseInMemoryDatabase("InMemoryDb"));
config.AddCommandLine(args);
})
.UseStartup<Startup>();
}
One approach to resolve your issue:
I guess you should set CopyToOutput to be TRUE on that central configuration file so that it gets copied to executable output.
Then you can load configuration just by calling AddJsonFile or AddXmlFile. Hope this helps you to resolve your issue.
This would also lessen your code and increase maintainability as there are no customization while loading configurations.
Hope this helps.
来源:https://stackoverflow.com/questions/54288247/asp-net-core-2-1-unable-to-run-migrations-due-to-environment-variable