.Net EntityFramework Core - SQLite Error 1: no such table

Viewed 2579

I am running into an issue where the code is unable to find my existing Sqlite database that is in the same folder as this code that is calling it. The error I am getting is, "SQLite Error 1 table does not exist." I know the table exists, it is just unable to find the path. What am I doing wrong?

Note: I am not creating a code first Sqlite database. This is opening an existing Sqlite database

DATABASE CONTEXT CODE

using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyWidget.Domain.Data
{
    public static class MyWidgetService
    {
        public static void GetAll()
        {            

            using var context = new MyWidgetContext();
            if (context.Translations.Any())
            {
                var data = context.Widgets.ToList(); 
                foreach (var widget in data)
                {
                    Console.WriteLine(widget.ProductName);
                }
            }
            else
            {
                Console.WriteLine("No widgets found");
            }
        }
    }
}

**DATABASE SERVICE CODE

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using Microsoft.EntityFrameworkCore;

namespace MyWidget.Domain.Data
{
    public class MyWidgetContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite(connectionString: "Filename=./MyWidgetDB.db");
        }

        public DbSet<WidgetData> Widgets { get; set; }
    }

    public class WidgetData
    {
        public int Id { get; set; }
        public string ProductName { get; set; }

    }
}
2 Answers

I had the same problem: SqliteException: SQLite Error 1: 'no such table: ScanFolders'. I used the SQLlite Browser to inspect the database in the debug folder and it had no tables. Checking the properties on the db was set to "Do Not Copy" I changed it to "Copy if Newer" and that fixed the issue for me.

Related