user9135351
user9135351

Reputation:

No MigrationSqlGenerator found for provider 'System.Data.SQLite'

I'm using SQLite with EntityFramework. When I try to run the command enable-migrations, I get this error.

No MigrationSqlGenerator found for provider 'System.Data.SQLite'. Use the >SetSqlGenerator method in the target migrations configuration class to >register additional SQL generators

Here is my DBContext and my DBConfiguration

public class SQLiteConfiguration : DbConfiguration
   {
      public SQLiteConfiguration()
      {
        SetProviderFactory("System.Data.SQLite", SQLiteFactory.Instance);
        SetProviderFactory("System.Data.SQLite.EF6", 
SQLiteProviderFactory.Instance);
        SetProviderServices("System.Data.SQLite", (DbProviderServices)SQLiteProviderFactory.Instance.GetService(typeof(DbProviderServices)));
    }
}
public class ApplicationContextDB : DbContext
{

    static private string dbpath;
    static ApplicationContextDB()
    {
        var exeDir = AppDomain.CurrentDomain.BaseDirectory;
        var exeDirInfo = new DirectoryInfo(exeDir);
        var projectDir = exeDirInfo.Parent.Parent.FullName;
        dbpath= $@"{projectDir}\DBFolder\MyDB.db";
    }

    public ApplicationContextDB() : base(new SQLiteConnection($"DATA Source={dbpath}"), false)
    {
    }


    public ApplicationContextDB(DbConnection connection) : base(connection, true)
    {
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        base.OnModelCreating(modelBuilder);
    }
    public DbSet<User> Users{ get; set; }

    public DbSet<Work> Works{ get; set; }
}

Upvotes: 6

Views: 4516

Answers (2)

ByByeBye
ByByeBye

Reputation: 161

link for your codes: https://qiita.com/minoru-nagasawa/items/961f6eae809a379c1b52

solution for your qusetion: https://github.com/minoru-nagasawa/SQLiteMigrationSample

Steps:

  1. PM> enable-migrations(gets error:No MigrationSqlGenerator found, ignore..this error,the sqlite db file has already generate at the dbfilepath)

  2. add this line in Configuration.cs (this step is the very important for the issue, and should be after the Step1):

public Configuration()
{
    AutomaticMigrationsEnabled = false;
    SetSqlGenerator("System.Data.SQLite", new SQLiteMigrationSqlGenerator());// the Golden Key
}
  1. PM> add-migration initOrWhatEverYourName

  2. PM> update-database

  3. DONE.

Upvotes: 9

Khatibzadeh
Khatibzadeh

Reputation: 470

SQLite provider for entity framework does not support SQL migration. The migration and database generation SQLs must be written manually for now

Upvotes: 0

Related Questions