简体   繁体   English

EF核心更新数据库错误“操作数类型冲突:日期与smallint不兼容”

[英]EF Core Update-Database error “Operand type clash: date is incompatible with smallint”

EF Core Update-Database failed when i try change column type from datetime to smallint: "Operand type clash: date is incompatible with smallint" 当我尝试将列类型从日期时间更改为smallint时,EF Core Update-Database失败:“操作数类型冲突:日期与smallint不兼容”

Migration file: 迁移文件:

public partial class BookYearFieldChanged : Migration 公共局部类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));
    }
}

create a new column with required datatype, update the new column with required data and drop the previous column. 创建具有所需数据类型的新列,使用所需数据更新新列,并删除前一列。

 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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM