在保留数据的同时更改模型中的数据类型(Entity Framework Core)

时间:2018-10-21 09:15:04

标签: c# sql entity-framework-core

我正在学习Entity Framework核心,我想执行迁移,将数据类型更改为自定义类型,同时保留数据。 我有以下模型:

public class PageDataSections
{
    [Key]
    public int PageDataSectionId { get; set; }
    public string DataText { get; set; }
}

将被转换为此:

public class PageDataSections
{
    [Key]
    public int PageDataSectionId { get; set; }
    public Translation DataText { get; set; }
}

Translation类如下:

public class Translation
{
    [Key]
    public int TranslationId { get; set; }
    public string TranslateEN { get; set; }
    public string TranslateRU { get; set; }
    public string TranslateUA { get; set; }
}

已创建的迁移:

public partial class ChangeDataTextColumn : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {

            migrationBuilder.AddColumn<int>(
                name: "DataTextTranslationId",
                table: "PageDataSections",
                nullable: true);

            migrationBuilder.DropColumn(
                name: "DataText",
                table: "PageDataSections");      

            migrationBuilder.CreateIndex(
                name: "IX_PageDataSections_DataTextTranslationId",
                table: "PageDataSections",
                column: "DataTextTranslationId");

            migrationBuilder.AddForeignKey(
                name: "FK_PageDataSections_Translations_DataTextTranslationId",
                table: "PageDataSections",
                column: "DataTextTranslationId",
                principalTable: "Translations",
                principalColumn: "TranslationId",
                onDelete: ReferentialAction.Restrict);

            migrationBuilder.Sql(@"
            INSERT INTO Translations(TranslateRU)
            OUTPUT inserted.TranslationId
            VALUES(SELECT DataText FROM PageDataSections)
            INTO PageDataSections(DataTextTranslationId)
");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_PageDataSections_Translations_DataTextTranslationId",
                table: "PageDataSections");

            migrationBuilder.DropIndex(
                name: "IX_PageDataSections_DataTextTranslationId",
                table: "PageDataSections");

            migrationBuilder.DropColumn(
                name: "DataTextTranslationId",
                table: "PageDataSections");

            migrationBuilder.AddColumn<string>(
                name: "DataText",
                table: "PageDataSections",
                nullable: true);
        }
    }

但是Visual Studio抱怨此错误:

fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
      Failed executing DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']

                  INSERT INTO Translations(TranslateRU)
                  OUTPUT inserted.TranslationId
                  VALUES(SELECT DataText FROM PageDataSections)
                  INTO PageDataSections(DataTextTranslationId)
System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near the keyword 'SELECT'.
Incorrect syntax near ')'.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
ClientConnectionId:14012b7c-68b2-4f43-9db1-b685734a2e37
Error Number:156,State:1,Class:15
Failed executing DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']

            INSERT INTO Translations(TranslateRU)
            OUTPUT inserted.TranslationId
            VALUES(SELECT DataText FROM PageDataSections)
            INTO PageDataSections(DataTextTranslationId)
System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near the keyword 'SELECT'.
Incorrect syntax near ')'.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
ClientConnectionId:14012b7c-68b2-4f43-9db1-b685734a2e37
Error Number:156,State:1,Class:15
Incorrect syntax near the keyword 'SELECT'.
Incorrect syntax near ')'.

有人可以帮忙吗?

编辑

@Fabio回复后,我尝试了他的代码

migrationBuilder.Sql(@"
    INSERT Translations (TranslateRU)
    OUTPUT inserted.TranslationId INTO PageDataSections(DataTextTranslationId)
    (SELECT DataText FROM PageDataSections) 
");

它给了我以下内容:

fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
      Failed executing DbCommand (231ms) [Parameters=[], CommandType='Text', CommandTimeout='30']

      INSERT Translations (TranslateRU)
          OUTPUT inserted.TranslationId INTO PageDataSections(DataTextTranslationId)
          (SELECT DataText FROM PageDataSections)
System.Data.SqlClient.SqlException (0x80131904): Invalid column name 'DataText'.
The target table 'PageDataSections' of the OUTPUT INTO clause cannot be on either side of a (primary key, foreign key) relationship. Found reference constraint 'FK_PageDataSections_Translations_DataTextTranslationId'.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
ClientConnectionId:3224c672-8dbd-4690-8953-ee6108b65cff
Error Number:207,State:1,Class:16
Failed executing DbCommand (231ms) [Parameters=[], CommandType='Text', CommandTimeout='30']

INSERT Translations (TranslateRU)
    OUTPUT inserted.TranslationId INTO PageDataSections(DataTextTranslationId)
    (SELECT DataText FROM PageDataSections)
System.Data.SqlClient.SqlException (0x80131904): Invalid column name 'DataText'.
The target table 'PageDataSections' of the OUTPUT INTO clause cannot be on either side of a (primary key, foreign key) relationship. Found reference constraint 'FK_PageDataSections_Translations_DataTextTranslationId'.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
ClientConnectionId:3224c672-8dbd-4690-8953-ee6108b65cff
Error Number:207,State:1,Class:16
Invalid column name 'DataText'.
The target table 'PageDataSections' of the OUTPUT INTO clause cannot be on either side of a (primary key, foreign key) relationship. Found reference constraint 'FK_PageDataSections_Translations_DataTextTranslationId'.

这似乎很奇怪,因为该列存在于表中: Database

更新

这是引用外键的方式enter image description here

更新 通过此查询,我取得了一些结果,但它没有修改行,而是添加了。如何“更新”而不是“插入”?

            migrationBuilder.Sql(@"ALTER TABLE Pages DROP CONSTRAINT FK_Pages_PageDataSections_PageDataSectionId");

            migrationBuilder.Sql(@"
                INSERT Translations (TranslateRU)
                OUTPUT inserted.TranslationId INTO PageDataSections(DataTextTranslationId)
                SELECT DataText FROM PageDataSections;
");


            migrationBuilder.Sql(@"ALTER TABLE Pages ADD CONSTRAINT FK_Pages_PageDataSections_PageDataSectionId FOREIGN KEY (PageDataSectionId) REFERENCES PageDataSections(PageDataSectionId)");

2 个答案:

答案 0 :(得分:1)

当问题很复杂时,我宁愿将其分开。

对于此迁移,我将进行2次更新:

  1. 创建并填充“翻译”表
  2. 填充PageDataSections表

1)创建并填充“翻译”表

请参阅临时列Translations.PageDataSectionId

public partial class Update1 : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AddColumn<int>(
            name: "PageDataSectionOrigin",
            table: "Translations",
            nullable: true);

        migrationBuilder.Sql(@"
            INSERT INTO Translations (TranslateRU, PageDataSectionOrigin)
            SELECT DataText, PageDataSectionId FROM PageDataSections
        ");
    }
}

2)填充PageDataSections表

看到临时列被删除。

public partial class Update2 : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropColumn(
            name: "DataText",
            table: "PageDataSections");

        migrationBuilder.AddColumn<int>(
            name: "DataTextTranslationId",
            table: "PageDataSections",
            nullable: true);

        migrationBuilder.Sql(@"
            UPDATE PageDataSections SET DataTextTranslationId = (select TranslationId FROM Translations WHERE PageDataSectionOrigin=PageDataSectionId)
        ");

        migrationBuilder.DropColumn(
            name: "PageDataSectionOrigin",
            table: "Translations");
    }
}

您可以将这两个更新合而为一。

我不知道为什么,但是EF将PageDataSection.DataText列命名为DataTextTranslationId。但是您可以轻松替换此名称。

答案 1 :(得分:0)

我认为您的sql语句无效。整个OUTPUT语句应介于INSERT和插入值之间。
此外,使用花药查询的结果时不需要VALUES关键字。

migrationBuilder.Sql(@"
    INSERT Translations (TranslateRU)
    OUTPUT inserted.TranslationId INTO PageDataSections(DataTextTranslationId)
    (SELECT DataText FROM PageDataSections) 
");