迁移未运行并引发__EFMigrationsHistory错误

时间:2019-06-25 11:19:47

标签: c# postgresql .net-core ef-core-2.1

我拥有Dot net Core 2.2.1和Entity Framework Core 2.1.3以及Npgsql.EntityFrameworkCore.PostgreSQL 2.0.0,我无法升级其中任何一个,因为这与支持dot net core 2.2.1的AWS Lambda函数有关。现在,当我运行Add-Migration InitialCreate时,它会创建迁移,而当我键入Update-Database时,则会出现以下错误

    fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
      Failed executing DbCommand (132ms) [Parameters=[], CommandType='Text', 
    CommandTimeout='30']
      INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
      VALUES (20190625105712_initialCreate, 2.1.8-servicing-32085);
Npgsql.PostgresException (0x80004005): 42601: syntax error at or near "_initialCreate"
   at Npgsql.NpgsqlConnector.DoReadMessage(Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isPrependedMessage)
   at Npgsql.NpgsqlConnector.ReadMessage(Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications)
   at Npgsql.NpgsqlConnector.ReadMessage(Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications)
   at Npgsql.NpgsqlConnector.ReadExpecting[T](Boolean async)
   at Npgsql.NpgsqlDataReader.NextResult(Boolean async)
   at Npgsql.NpgsqlDataReader.NextResult()
   at Npgsql.NpgsqlCommand.Execute(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteNonQuery(Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteNonQuery()
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
Failed executing DbCommand (132ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
VALUES (20190625105712_initialCreate, 2.1.8-servicing-32085);
Npgsql.PostgresException (0x80004005): 42601: syntax error at or near "_initialCreate"
   at Npgsql.NpgsqlConnector.DoReadMessage(Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isPrependedMessage)
   at Npgsql.NpgsqlConnector.ReadMessage(Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications)
   at Npgsql.NpgsqlConnector.ReadMessage(Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications)
   at Npgsql.NpgsqlConnector.ReadExpecting[T](Boolean async)
   at Npgsql.NpgsqlDataReader.NextResult(Boolean async)
   at Npgsql.NpgsqlDataReader.NextResult()

我的dbcontext类是

public class TraceUIContext : DbContext
    {
        public TraceUIContext(DbContextOptions<TraceUIContext> options):base(options)
        {

        }
        public DbSet<Alarms> Alarms { get; set; }
    }

我尝试创建脚本文件,并将文件更改为INSERT INTO“ __EFMigrationsHistory”(“ MigrationId”,“ ProductVersion”) 值('20190625105712_initialCreate','2.1.8-servicing-32085'); 它成功执行。我认为在运行迁移时,它会以字符串形式生成Values,但由于整个迁移都会失败,因此不会添加单引号。

迁移产生的代码为

public partial class initialCreate : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Alarms",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn),
                    GroupNo = table.Column<long>(nullable: false),
                    GroupN = table.Column<string>(maxLength: 20, nullable: true),
                    ChannelNo = table.Column<long>(nullable: false),
                    ChannelN = table.Column<string>(maxLength: 10, nullable: true),
                    ComputerName = table.Column<string>(maxLength: 40, nullable: true),
                    AlarmType = table.Column<string>(maxLength: 20, nullable: true),
                    AlarmSetPoint = table.Column<string>(maxLength: 10, nullable: true),
                    AlarmStart = table.Column<DateTime>(nullable: false),
                    AlarmAcknowleged = table.Column<DateTime>(nullable: false),
                    AlarmAcknowledgedBy = table.Column<string>(maxLength: 10, nullable: true),
                    AlarmEnd = table.Column<DateTime>(nullable: false),
                    AlarmReason = table.Column<string>(maxLength: 40, nullable: true),
                    AlarmAction = table.Column<string>(maxLength: 40, nullable: true),
                    AlarmValue = table.Column<string>(maxLength: 10, nullable: true),
                    AlarmDelay = table.Column<string>(maxLength: 7, nullable: true),
                    AlarmStatus = table.Column<string>(maxLength: 16, nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Alarms", x => x.Id);
                });
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "Alarms");
        }
    }

它应该运行迁移并且应该能够创建没有问题的脚本。

0 个答案:

没有答案