【问题标题】:No MigrationSqlGenerator found for provider 'System.Data.SQLite'找不到提供程序“System.Data.SQLite”的 MigrationSqlGenerator
【发布时间】:2019-05-20 19:23:54
【问题描述】:

我正在使用 SQLiteEntityFramework。 当我尝试运行命令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; }
}

【问题讨论】:

    标签: c# entity-framework sqlite


    【解决方案1】:

    您的代码链接: 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
    }
    
    1. PM> add-migration initOrWhatEverYourName

    2. PM> 更新数据库

    3. 完成。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 嗯,好的。非常感谢。我要试试。我正在寻找添加表格的快捷方式:)
      猜你喜欢
      • 1970-01-01
      • 2020-04-12
      • 1970-01-01
      • 1970-01-01
      • 2013-06-25
      • 2023-01-31
      • 2021-12-15
      • 2015-04-29
      • 1970-01-01
      相关资源
      最近更新 更多