I have a simple question. I have 2 layers in my application, a front-end and data access layer, in different projects. I am creating a sqlite db in the data access layer by migration in data access layer and now I want to use a connection string. I am creating a context in the data access layer like this:
public class TodoDbContext : DbContext
{
public DbSet<Activity> Activieties { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite(@"Data Source=todo.db");
}
}
Looks simple right, db is created in data access layer project. But when I run front end project program is looking for a db but in a front end project folder.I have checke that by adding:
var test = Directory.GetCurrentDirectory();
In above method. Also when I modify connection string to direct path like:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite(@"Data Source=C:\Users\Administrator\Desktop\TodoList\TodoDataAccess\todo.db");
}
It works, so my question is what can I do to change it?
SQLite can handle multiple applications reading the db at the same time, but not writing to it. From the SQLite FAQ: Multiple processes can have the same database open at the same time. Multiple processes can be doing a SELECT at the same time.
SQLite allows multiple processes to have the database file open at once, and for multiple processes to read the database at once. When any process wants to write, it must lock the entire database file for the duration of its update. But that normally only takes a few milliseconds.
SQLite supports an unlimited number of simultaneous readers, but it will only allow one writer at any instant in time. For many situations, this is not a problem. Writers queue up. Each application does its database work quickly and moves on, and no lock lasts for more than a few dozen milliseconds.
Overview. Usually, SQLite allows at most one writer to proceed concurrently. The BEGIN CONCURRENT enhancement allows multiple writers to process write transactions simultanously if the database is in "wal" or "wal2" mode, although the system still serializes COMMIT commands.
To make accessing your app data easier just put it in well known folder. Based on your application name I assume that your app is a desktop one and stores user specific data in SQLite DB which should be accessible from different computers. Than location of DB should be determined as:
var sqlitePath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
@"<YourAppName>\todo.db");
This simplifies all application data management and makes location of the data on all computers users may be using consistent disregard of the way application is installed. Furthermore this guarantees that user will have full access rights to DB folder. In case your project has other functional requirements data location should be adjusted accordingly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With