Home > Net >  Entity Framework SQLite Error 1: 'no such table: __EFMigrationsHistory'
Entity Framework SQLite Error 1: 'no such table: __EFMigrationsHistory'

Time:08-28

I am running EF Core on Windows and Linux and have same issue on both.

public string DbPath { get; }

string DbPath = $ "{Environment.GetFolderPath(Environment.SpecialFolder.Personal)}{Path.DirectorySeparatorChar}smart_contracts.db";

This prints the path on Linux and on Windows 10. When I try to run Add-Migration and Update-Database I get the SQLite error

Error 1: 'no such table: __EFMigrationsHistory'. error.

Code:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder.UseSqlite($"Data Source={DbPath}");
}

If I change it to Data Source=smart_contracts.db, it works perfectly on both platforms.

Can anyone help me get a path into the data source string?

CodePudding user response:

It seems to me that the problem is that you want to perform a migration on a database that is already structured and not generated by the Entity Framework. This would explain the lack of the migration table. To use this database you should reverse engineer the database structure. In case you do not want to change the structure either on the database side or on the application side (because, let's say you implemented the entities yourself), all you need to do is to specify the localization of the database file when configuring DbContext (as it is shown in your post) and that's it.

Here a example of Scaffolding a SQLite database:

Scaffold-DbContext "DataSource=file.sqlite3 "Microsoft.EntityFrameworkCore.Sqlite -OutputDir Models

Edit: The problem apparently stems from the fact of how you combine the path to the database file. The better practice is to use the Path.Combine() method instead of string concatenation.

  • Related