The ‘‘ property on ‘‘ could not be set to a ‘System.String‘ value. You must set this property to

    技术2022-07-11  97

    VS中报错误如下所示: The ‘AccountIdentity’ property on ‘T_TRACE’ could not be set to a ‘System.String’ value. You must set this property to a non-null value of type ‘System.Int64’.

    翻译如下: “TRACE”上的“AccountIdentity”属性无法设置为’System.String’类型。必须将此属性类型设置为“System.Int64”且不为空的值;(意思就是说VS中设置的属性类型为Int64,但是数据库中属性类型是string类型(Slserver中是varchar类型),两个地方的数据类型不一致导致的错误)

    数据库表设计如下图所示:(表名:T_Trace) 下图是VS中的Model

    执行如下代码:

    return View(traceInfo.OrderByDescending(t => t.RequestDate).ThenByDescending(t => t.TraceKey).ToPagedList(page, pageSize));

    VS报错截图如下图所示:

    解决方法

    原因: Model中的字段类型和 数据库表 中的数据类型不对应。 Model中被定义成了long,而数据库中的是varchar(128)对应的Model应该是string类型.

    解决方法: 数据库不动,则在VS中把下面的代码

    public long AccountIdentity { get; set; }

    改成

    public string AccountIdentity { get; set; }
    Processed: 0.013, SQL: 10