Home > OS >  Entity Framework SQLite Error 1: 'no such table: __EFMigrationsHistory' - Database is not
Entity Framework SQLite Error 1: 'no such table: __EFMigrationsHistory' - Database is not

Time:01-19

I am trying to follow this tutorial

initially dotnet ef migrations add initialmigration did not work. (could not find command)

I updated VS2022 to 17.4.4 , and ran dotnet tool install --global dotnet-ef and then was able to create the initial migration.

I am now getting and error when running dotnet ef database update

The error I get is

PS D:\Soft\Source\ToDoApi\todoapi> dotnet ef database update Build started... Build succeeded. info: Microsoft.EntityFrameworkCore.Database.Command[20101] Executed DbCommand (9ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] SELECT COUNT() FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table'; info: Microsoft.EntityFrameworkCore.Database.Command[20101] Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] CREATE TABLE "__EFMigrationsHistory" ( "MigrationId" TEXT NOT NULL CONSTRAINT "PK___EFMigrationsHistory" PRIMARY KEY, "ProductVersion" TEXT NOT NULL ); info: Microsoft.EntityFrameworkCore.Database.Command[20101] Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] SELECT COUNT() FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table'; info: Microsoft.EntityFrameworkCore.Migrations[20402] Applying migration '20230117171527_InitialMigration'. Applying migration '20230117171527_InitialMigration'. info: Microsoft.EntityFrameworkCore.Database.Command[20101] Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] CREATE TABLE "ToDos" ( "Id" INTEGER NOT NULL CONSTRAINT "PK_ToDos" PRIMARY KEY AUTOINCREMENT, "ToDoName" TEXT NULL ); fail: Microsoft.EntityFrameworkCore.Database.Command[20102] Failed executing DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion") VALUES ('20230117171527_InitialMigration', '7.0.2'); Failed executing DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion") VALUES ('20230117171527_InitialMigration', '7.0.2'); Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 1: 'no such table: __EFMigrationsHistory'.

I do not see a sqlite database file, or reference to it in the solution explorer although it seemed to have done a select count(*) from sqlite_master table without failing?

It looks like the database was not created

I looked at This SO question which gives the same error due to not finding the path to the DB - not sure if this could also be my problem - but how to resolve it?

I have tried removing the migration: dotnet ef migrations remove and adding it again but I still get the same error when I run dotnet ef database update

I tried updating the dotnet tool dotnet ef update --global dotnet-ef (Tool 'dotnet-ef' was reinstalled with the latest stable version (version '7.0.2').)

I have these nuget packages installed:

a) Microsoft.EntityFrameworkCore.Design 7.0.2

b) Microsoft.EntityFrameworkCore.Sqlite 7.0.2

Any ideas?

Edit

@art Here are the appsettings.json

   {
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    },
    "ConnectionStrings": {
      "SqliteConnection": "Data Source=Todo.db"
    }
  }
}

and program.cs

using Microsoft.EntityFrameworkCore;
using ToDoAPI.Data;
using ToDoAPI.Models;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddDbContext<AppDbContext>(opt =>
    opt.UseSqlite(builder.Configuration.GetConnectionString("SqliteConnection")));

var app = builder.Build();


app.Run();

and AppDbContext

using Microsoft.EntityFrameworkCore;
using ToDoAPI.Models;

namespace ToDoAPI.Data
{
    public class AppDbContext : DbContext
    {
        public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
        {

        }

        public DbSet<ToDo> ToDos => Set<ToDo>();
    }
}

CodePudding user response:

In your appsettings.json hierarchy "ConnectionStrings" is under "Logging"

{
    "Logging": {
        ... ,
        "ConnectionStrings": { ... }
    }
}

Thats why program can't find path to DB

"ConnectionStrings" should be next to "Logging"

{
    "Logging": { ... },
    "ConnectionStrings": { ... }
}
  • Related