将现有的基于磁盘的表迁移到内存优化表

时间:2019-05-16 14:35:44

标签: sql-server entity-framework-core memory-optimized-tables

我们正在使用ASP.NET MVC Core和EntityFramework Core开发用于访问数据的Web应用程序。 .net核心版本是2.2。现在,我们试图将SQL Server 2016中的MemoryOptimizedTables用于现有表。

我在OnModelCreating方法中添加了以下行 modelBuilder.Entity<MailLog>().ForSqlServerIsMemoryOptimized(); 然后我执行了Add-Migration,结果是下面的迁移文件。

public partial class DataModel_v0711 : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropPrimaryKey(
                name: "PK_Core_MailLogs",
                table: "Core_MailLogs");

            migrationBuilder.AlterDatabase()
                .Annotation("SqlServer:MemoryOptimized", true);

            migrationBuilder.AlterTable(
                name: "Core_MailLogs")
                .Annotation("SqlServer:MemoryOptimized", true);

            migrationBuilder.AddPrimaryKey(
                name: "PK_Core_MailLogs",
                table: "Core_MailLogs",
                column: "Id")
                .Annotation("SqlServer:Clustered", false);
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropPrimaryKey(
                name: "PK_Core_MailLogs",
                table: "Core_MailLogs");

            migrationBuilder.AlterDatabase()
                .OldAnnotation("SqlServer:MemoryOptimized", true);

            migrationBuilder.AlterTable(
                name: "Core_MailLogs")
                .OldAnnotation("SqlServer:MemoryOptimized", true);

            migrationBuilder.AddPrimaryKey(
                name: "PK_Core_MailLogs",
                table: "Core_MailLogs",
                column: "Id");
        }
    }

然后我执行了更新数据库命令。我尝试使用空数据库。 我尝试使用以下命令获取迁移脚本。

dotnet ef migrations script  --idempotent -o c:\test\migrate2_1.sql --project EntityFrameworkCore.csproj --startup-project Web.Mvc.csproj

每次我遇到以下错误。

System.InvalidOperationException: To set memory-optimized on a table on or off the table needs to be dropped and recreated.
   at Microsoft.EntityFrameworkCore.Migrations.SqlServerMigrationsSqlGenerator.Generate(AlterTableOperation operation, IModel model, MigrationCommandListBuilder builder)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(MigrationOperation operation, IModel model, MigrationCommandListBuilder builder)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(IReadOnlyList`1 operations, IModel model)
   at Microsoft.EntityFrameworkCore.Migrations.SqlServerMigrationsSqlGenerator.Generate(IReadOnlyList`1 operations, IModel model)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GenerateUpSql(Migration migration)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GenerateScript(String fromMigration, String toMigration, Boolean idempotent)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.ScriptMigration(String fromMigration, String toMigration, Boolean idempotent, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScriptMigration.c__DisplayClass0_1.b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.c__DisplayClass3_0`1.b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
To set memory-optimized on a table on or off the table needs to be dropped and recreated.

谢谢。

2 个答案:

答案 0 :(得分:0)

看起来像实体框架不够聪明,无法生成用于移动数据的脚本。 请尝试:
1.使用EF创建全新的空数据库。
2.使用Visual Studio架构比较或RedGate等其他工具生成更改脚本。您也可以手动创建一个。
3.手动更新数据库(或使用手动迁移)

答案 1 :(得分:0)

查看我的解决方案

https://www.red-gate.com/simple-talk/sql/t-sql-programming/converting-database-memory-oltp/

该解决方案是免费的! 基于基于磁盘的数据库,将创建InMemory数据库。无需手动更新。