找不到提供程序“System.Data.SQLite”的 MigrationSqlGenerator

2 c# sqlite entity-framework

我正在SQLite与EntityFramework. 当我尝试运行命令时enable-migrations,出现此错误。

找不到提供程序“System.Data.SQLite”的 MigrationSqlGenerator。使用目标迁移配置类中的 >SetSqlGenerator 方法来>注册其他 SQL 生成器

这是我的 DBContext和我的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; }
}
Run Code Online (Sandbox Code Playgroud)

ByB*_*Bye 5

代码链接:https : //qiita.com/minoru-nagasawa/items/961f6eae809a379c1b52

您的问题的解决方案:https : //github.com/minoru-nagasawa/SQLiteMigrationSample

脚步:

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

  2. 在Configuration.cs中添加这一行(这一步对于问题很重要,应该在Step1之后):

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

  2. PM> 更新数据库

  3. 完毕。


小智 0

实体框架的 SQLite 提供程序不支持 SQL 迁移。目前迁移和数据库生成SQL必须手动编写