EF Core(Entity Framework Core)与SQLite的迁移,以及SQlite在EF中的局限性及其解决方案

建立迁移

使用包管理控制台

Add-Migration 迁移名称

(比如 Add-Migration DatabaseInformation_Extension)

如果产生冲突可以使用:

Update-Database -Migration:0 (删除表结构)

在Program处,服务启动之前添加调用函数

private static void CreateDbIfNotExists(IHost host)
{
	using var scope = host.Services.CreateScope();
	var services = scope.ServiceProvider;
	using var context = scope.ServiceProvider.GetRequiredService<BeeNetContext>();
	context.Database.Migrate();
}

服务器启动时候会自动完成迁移。

SQLite - SQLite 局限性 - 《微软 EntityFrameworkCore 中文文档》 - 书栈网 · BookStack
https://www.bookstack.cn/read/Microsoft.EntityFrameworkCore.Docs.zh-Hans/7%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93%E6%8F%90%E4%BE%9B%E7%A8%8B%E5%BA%8F-C%E3%80%81SQLite-B%E3%80%81SQLite%E5%B1%80%E9%99%90%E6%80%A7.md

关于SQLite不支持迁移的问题可以查看一下:

// UNDONE: Not supported by SQLite
//migrationBuilder.AlterColumn<string>(
//    name: "Title",
//    table: "Posts",
//    nullable: false,
//    oldClrType: typeof(string),
//    oldType: "TEXT",
//    oldNullable: true);

// Create a new table with the desired schema
// TODO: Randomize the name to avoid conflicts
migrationBuilder.CreateTable(
    name: "new_Posts",
    columns: table => new
    {
        Id = table.Column<int>(nullable: false)
            .Annotation("Sqlite:Autoincrement", true),
        Title = table.Column<string>(nullable: false)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_Posts", x => x.Id);
    });

// Copy data from the old table. Use NULLIF to specify a default value for newly
// required columns
migrationBuilder.Sql(@"
    INSERT INTO new_Posts (Id, Title)
    SELECT Id, IFNULL(Title, '')
    FROM Posts;
");

// Suspend foreign key enforcement during the swap
// TODO: Can't do this on SQL Server. Would need to rebuild referencing foreign
// keys there. But do we even need table rebuilds on SQL Server? Changing
// IDENTITY only requires a column rebuild
// NB: This commits the current transaction. We can't rollback the migration if
// anything after this fails. Maybe we can turn this off before the migration
// and somehow use PRAGMA foreign_key_check. Otherwise, we can mitigate it by
// doing rebuilds as late as possible
migrationBuilder.Sql("PRAGMA foreign_keys = 0;", suppressTransaction: true);

// Swap in the new table
migrationBuilder.DropTable(
    name: "Posts");
migrationBuilder.RenameTable(
    name: "new_Posts",
    newName: "Posts");

// TODO: We shouldn't do this if foreign key enforcement was off to begin with.
// There's no way to handle this in SQL, so we probably need a way to configure
// this. Maybe we can do this as a post migration step--the migration wouldn't
// fail, but you'd at least get an error if you compromised referential
// integrity
migrationBuilder.Sql("PRAGMA foreign_keys = 1;", suppressTransaction: true);

// Rebuild any indexes
migrationBuilder.CreateIndex(
    name: "IX_Posts_Title",
    table: "Posts",
    column: "Title");
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值