Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I point two different projects to the same SQLite db-file?

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?

like image 371
Wojciech Szabowicz Avatar asked Nov 08 '17 11:11

Wojciech Szabowicz


People also ask

Can multiple applications use the same SQLite database?

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.

Can SQLite be access by multiple processes?

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.

How many concurrent connections can SQLite handle?

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.

Does SQLite support concurrency?

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.


1 Answers

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.

like image 126
Jacek Blaszczynski Avatar answered Sep 19 '22 13:09

Jacek Blaszczynski