PraiseJourney
PraiseJourney

Reputation: 57

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

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; }

    }
}

Upvotes: 2

Views: 5204

Answers (2)

Se&#225;n Gahan
Se&#225;n Gahan

Reputation: 84

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.

Upvotes: 1

PraiseJourney
PraiseJourney

Reputation: 57

I decided to go another direction and use Dapper and System.Data.Sqlite.Core. This video by Tim Corey perfectly explains how to implement it:

https://www.youtube.com/watch?v=ayp3tHEkRc0

This methodology works really well and am using it in my GitHub project:

https://github.com/encouragingapps/Blender3DReference

Upvotes: 1

Related Questions