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
I want to change this.
I tried setting
IHostingEnvironment.ContentRootPath. Nothing works.
How do I programmatically set the directory that the database should be created 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);