我正在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)
代码链接:https : //qiita.com/minoru-nagasawa/items/961f6eae809a379c1b52
您的问题的解决方案:https : //github.com/minoru-nagasawa/SQLiteMigrationSample
脚步:
PM> enable-migrations(gets error:No MigrationSqlGenerator found, ignore..this error, the sqlite db file has already generate at the dbfilepath)
在Configuration.cs中添加这一行(这一步对于问题很重要,应该在Step1之后):
public Configuration()
{
AutomaticMigrationsEnabled = false;
SetSqlGenerator("System.Data.SQLite", new SQLiteMigrationSqlGenerator());// the Golden Key
}
Run Code Online (Sandbox Code Playgroud)
PM> add-migration initOrWhatEverYourName
PM> 更新数据库
完毕。
| 归档时间: |
|
| 查看次数: |
1978 次 |
| 最近记录: |