Строка подключения Sqlite в папке AppData. Entity Framework Core - PullRequest
0 голосов
/ 06 января 2019

Я делаю приложение, которое использует sqlite в качестве localdb.

Я хочу сохранить базу данных в файле appdata пользователя.

Вот мой код:

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace cctv_tech.Models
{
    class DatabaseContext : DbContext
    {
        public DbSet<Setting> Settings { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Data Source=mydb.db;"); 
            // this is working, but database file is within the debug folder

            optionsBuilder.UseSqlite("Data Source=c:\\mydb.db;");
            // this output me an error which the database cannot be open,
            // and when checked there is no such file
        }
    }
}

SQLite Error 14

Надеюсь, кто-нибудь может помочь. Большое спасибо.

1 Ответ

0 голосов
/ 06 января 2019

Возможно, в основной программе вы просто отсутствовали:

    using (var scope = host.Services.CreateScope())
    {
       var context = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
       context.Database.EnsureCreated();
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...