在使用Code First创建MySQL数据时,报错提示“Unable to cast object of type 'System.Boolean' to type 'System.Int16'.”
这是因为MySQL引擎在处理tinyint(1)时默认会将其当作Boolean处理。
解决方法是在实体类中bool属性前面加上[Column(TypeName = "BIT")]
如下所示:
[Required]
[Column(TypeName = "BIT")]
public bool IsDelete { get; set; }
网上还有种解决办法是在连接字符串中添加参数"TreatTinyAsBoolean=false"。不过我测试之后并没有效果。
文章评论