在迁移过程中忽略SQLite的模式属性
本文关键字:模式 属性 SQLite 迁移 过程中 | 更新日期: 2023-09-27 18:11:31
我有一个共享模型,将用于创建SqlServer和SQLite数据库。我为每个模型类添加了Table属性,并提供了一个模式:
[Table("Sites", Schema = "Common")]
当我使用context.Database.Migrate()
迁移SQLite数据库时,我得到一个NotSupportedException错误。我知道SQLite不支持模式,所以我想知道是否有一种简单的方法可以在迁移期间忽略模式属性?
用代码而不是通过属性设置模式。这样,您就可以使用一些方法/config来确定模式,例如:
public class MyContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
if(UsingSqlLite)
{
modelBuilder.Entity<Site>().ToTable("Sites");
}
else
{
modelBuilder.Entity<Site>().ToTable("Sites", "Common");
}
}
//snip
}
由于您在SQL Server和SQLite上使用相同的迁移,您可能需要忽略迁移中的模式。
protected override void Up(MigrationBuilder migrationBuilder)
{
if (migrationBuilder.ActiveProvider == "Microsoft.EntityFrameworkCore.Sqlite")
{
migrationBuilder.CreateTable(
name: "Sites",
...);
}
else
{
migrationBuilder.CreateTable(
name: "Sites",
schema: "Common",
...);
}
}