如何将两个不同的项目指向同一个SQLite数据库文件? [英] How can I point two different projects to the same SQLite db-file?

查看:122
本文介绍了如何将两个不同的项目指向同一个SQLite数据库文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题。我的应用程序中有2个层,分别是不同项目中的前端和数据访问层。我正在通过在数据访问层中进行迁移在数据访问层中创建一个sqlite db,现在我想使用连接字符串。我正在像这样的数据访问层中创建一个上下文:

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 DB中,该数据应可从其他计算机访问。数据库的位置应确定为:

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

这简化了所有应用程序数据管理,并使数据在所有计算机上的位置用户可能一直在使用一致的忽略应用程序的安装方式。此外,这保证了用户将具有对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.

这篇关于如何将两个不同的项目指向同一个SQLite数据库文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆