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

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)
The ‘ ‘ property on ‘ ‘ could not be set to a ‘System.String‘ value. You must set this property to_第1张图片
下图是VS中的Model
The ‘ ‘ property on ‘ ‘ could not be set to a ‘System.String‘ value. You must set this property to_第2张图片

执行如下代码:

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

VS报错截图如下图所示:

The ‘ ‘ property on ‘ ‘ could not be set to a ‘System.String‘ value. You must set this property to_第3张图片

解决方法

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

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

public long AccountIdentity { get; set; }

改成

public string AccountIdentity { get; set; }

你可能感兴趣的:(ASP.NET学习)