I cannot configure a working ODBC connection string for SQLite in aspsettings.json for .NET CORE Razor Pages

0

I have a Visual Studio 2017 .NET 2.2 Core Razor Pages project I have downloaded and installed the ODBC sqlite3 driver. I can see my sqlite database in Visual Studio Server Explorer and I can open tables and see rows. But for the life of me I cannot create a connection string that works.

I keep getting an error when clicking on the link to the page I want to display: "Course table not found". The error happens on the OnGetAsync :

namespace OESAC.Pages.Courses { public class IndexModel : PageModel { private readonly OESAC.Data.MyDbContext _context;

public IndexModel(OESAC.Data.MyDbContext context)
{
    _context = context;
}

public IList<CoursesViewModel> CoursesVM { get;set; }

public async Task OnGetAsync()
{
    //CoursesVM = await _context.Courses.ToListAsync();
    CoursesVM = await _context.Courses
        .Select(p => new CoursesViewModel
        {
            OESACID = p.OESACID,
            CourseTitle = p.CourseTitle,
            Instructor = p.Instructor,
            Locations = p.Locations,

Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 1: 'no such table: Courses'. at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db) at Microsoft.Data.Sqlite.SqliteCommand.PrepareAndEnumerateStatements(Stopwatch timer)+MoveNext() at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior) at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken) at Microsoft.Data.Sqlite.SqliteCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)

It happens no matter how I configure the ConnectionString in aspsetting.json.

I have tried every way I could find. Including just a plain path, a relative path, the string I can get when looking at the Connection I configured in VS that lets me see the tables and data. This is the connection string it gives me when configuring the ODBC driver for Sqlite:

"DefaultConnection": "Dsn=SQLite3 Datasource;database=J:\OESAC\Data\sqlite\oesac_new.db;stepapi=0;syncpragma=NORMAL;notxn=0;timeout=100000;shortnames=0;longnames=0;nocreat=1;nowchar=0;fksupport=0;oemcp=0;bigint=0;jdconv=0"

Here is a skinnier version I tried from this website (connectionstrings.com).

"DefaultConnection": "DRIVER=SQLite3 ODBC Driver;Database=J:\OESAC\Data\oesac_new.db;LongNames=0;Timeout=1000;NoTXN=0;SyncPragma=NORMAL;StepAPI=0;"

asp.net-core
sqlite
razor-pages
asked on Stack Overflow Apr 1, 2019 by JustJohn

1 Answer

0

Well I am a happy man. After 4+ days of trying to make a simple connection to sqlite for my scaffolded pages I found the solution, which is more a result of continuing to poke around on StackOverknow for something/anything that would help. On this post, someone was looking for a relative path to use, but gave an absolute path as an example that worked: Point to a differnty location of sqlite db file in .net.core and entity framework

In my context file where the problem was, MyDbContext : DbContext I have a crazy different syntax. Funny how it uses "Datasource=" name for just an absolute path string:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{   
    optionsBuilder.UseSqlite(@"Data source=J:\OESAC\Data\sqlite\oesac_new.db");
}

Totally crazy references based on I-don't-know-what. Anyway, when I changed OnConfiguring to match what the other guy was trying to refine into a relative path, all of a sudden everything worked for my Courses table scaffolding: Display, Add, Edit, Delete, Save. Whew. I was getting ready to use something big and clunky to manage a simple 2 table database.

answered on Stack Overflow Apr 3, 2019 by JustJohn • edited Apr 4, 2019 by JustJohn

User contributions licensed under CC BY-SA 3.0