问题
I'm using EF Core, to manage a SQLite database file, in a console project.
When I create a database, the file is created in the bin folder, so basically MyProjectName\bin\Debug\netcoreapp1.1\FooDatabase.sqlite
.
I want to change this.
I tried setting Directory.SetCurrentDirectory
and IHostingEnvironment.ContentRootPath
. Nothing works.
How do I programmatically set the directory that the database should be created in?
回答1:
In Microsoft.Data.Sqlite
2.0 (not yet released), Directory.SetCurrentDirectory()
will do the trick.
Until then I suggest using an absolute path in your connection string:
var builder = new SqliteConnectionStringBuilder(relativeConnectionString);
bilder.DataSource = Path.Combine(dataDirectory, bilder.DataSource);
var absoluteConnectionString = builder.ToString();
If you really want to fiddle with the internal mechanics, you can, but I wouldn't recommend it:
// .NET Framework
AppDomain.CurrentDomain.SetData("DataDirectory", myDataDirectory);
// .NET Core
Environment.SetEnvironmentVariable("ADONET_DATA_DIR", myDataDirectory);
来源:https://stackoverflow.com/questions/42541767/programmatically-change-path-for-ef-core-database-in-console-project