3

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); }

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

1 Comment

note that this ends looking inside the bin folder for the database
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.