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

.net-core dbcontext entity-framework-core file sqlite

Question

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

    }
}
1
0
4/5/2020 3:35:55 PM

Accepted Answer

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

0
4/12/2020 12:57:40 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow