【问题标题】:EF Core Update-Database error "Operand type clash: date is incompatible with smallint"EF Core 更新数据库错误“操作数类型冲突:日期与 smallint 不兼容”
【发布时间】:2017-05-23 21:30:10
【问题描述】:

当我尝试将列类型从 datetime 更改为 smallint 时,EF Core 更新数据库失败:“操作数类型冲突:日期与 smallint 不兼容”

迁移文件:

公共部分类 BookYearFieldChanged:迁移

{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AlterColumn<short>(
            name: "Published",
            table: "Books",
            nullable: false,
            oldClrType: typeof(DateTime),
            oldType: "date");
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AlterColumn<DateTime>(
            name: "Published",
            table: "Books",
            type: "date",
            nullable: false,
            oldClrType: typeof(short));
    }
}

【问题讨论】:

标签: c# sql sql-server entity-framework


【解决方案1】:

创建具有所需数据类型的新列,使用所需数据更新新列并删除前一列。

CREATE TABLE [dbo].[table1](
	[columr1] [nvarchar](100) NULL,
	[DTime] [datetime] NULL
) 

/*	you can't do like this*/
/*	alter table table1
	alter column Dtime smallint */
 
ALTER TABLE table1 
ADD DTM smallint

UPDATE table1 
SET DTM = CAST(DTime as smallint)

ALTER TABLE table1 
DROP column DTime

sp_rename 'table1.DTM','DTime','column'
GO

【讨论】:

  • 感谢您的回答,我知道,这可以用 SQL 完成。但我尝试使用 EF 命令“Add-Migration”和“Update-Database”来做到这一点。我很感兴趣,我可以用脚手架的迁移文件做些事情吗,做这个改变没有错误
猜你喜欢
  • 1970-01-01
  • 2021-04-15
  • 2014-03-01
  • 1970-01-01
  • 2019-10-31
  • 2016-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多