Programmatically change path for EF Core database in console project

c# entity-framework entity-framework-core

Question

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
0
3/1/2017 8:48:48 PM

Accepted Answer

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);
1
3/3/2017 5:00:29 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow