Reputation: 381
My DatabaseContext
has:
string physicalPath = "Filename=" + Path.Combine(Path.Combine(Directory.GetCurrentDirectory(), "Data"), "Database.db");
optionsBuilder.UseSqlite(physicalPath);
My Startup
has:
using (var client = new DatabaseContext())
{
client.Database.EnsureCreated();
}
It works perfectly with "D:\\Database.db"
but that's not very portable so I need to use its own directories.
Has anyone any idea?
Update: I tried again with a tutorial here and got same error in an exception page.
"An unhandled exception occurred while processing the request. SqliteException: SQLite Error 14: 'unable to open database file'. Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(int rc, sqlite3 db)"
Update: I get "System.UnauthorizedAccessException: 'Access to the path 'C:\Program Files\IIS Express\test.txt' is denied.'" when I try
string path = Path.Combine(Directory.GetCurrentDirectory(), "test.txt");
using (FileStream fs = File.Create(path)) { Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file."); // Add some information to the file. fs.Write(info, 0, info.Length); }
Upvotes: 3
Views: 8292
Reputation: 1
You need to put your Database under App_Data folder. All the SQLite required dll need to be in bin folder. Also the SQLite.Interop.dll
need to be in x64 and x86 folder under bin.
Upvotes: 0
Reputation: 39
How about using the following to construct the path that points to your DB file?:
// Gets the current path (executing assembly)
string currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
// Your DB filename
string dbFileName = "stuff.db";
// Creates a full path that contains your DB file
string absolutePath = Path.Combine(currentPath, dbFileName);
Do note that you will need to include the following using
statements in your DatabaseContext
:
using System.IO;
using System.Reflection;
Upvotes: 1