gpt4 book ai didi

c# - 找不到提供程序 'System.Data.SQLite' 的 MigrationSqlGenerator

转载 作者:行者123 更新时间:2023-12-02 03:15:36 28 4
gpt4 key购买 nike

我正在使用 SQLiteEntityFramework。当我尝试运行命令 enable-migrations 时,出现此错误。

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

这是我的 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; }
}

最佳答案

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

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

步骤:

  1. PM> 启用迁移(获取错误:未找到 MigrationSqlGenerator,忽略..此错误,sqlite db 文件已在 dbfilepath 生成)

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

public Configuration()
{
AutomaticMigrationsEnabled = false;
SetSqlGenerator("System.Data.SQLite", new SQLiteMigrationSqlGenerator());// the Golden Key
}
  1. PM> 添加迁移 initOrWhatEverYourName

  2. PM>更新数据库

  3. 完成。

关于c# - 找不到提供程序 'System.Data.SQLite' 的 MigrationSqlGenerator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56226978/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com