Pomelo.EntityFrameworkCore.MySql 报错 Unable to cast object of type 'System.Guid' to type 'System.String'

先说下环境:

  连接的是旧数据库而不是新生成的库,所以有很多不能删的旧数据,表主键是char(32),在使用Pomelo.EntityFrameworkCore.MySql跑起来后就报错了。

解决方式:在数据库连接串加上 oldguids=true

说下原因:Pomelo.EntityFrameworkCore.MySql  使用 MySqlConnector 作为mysql数据连接层。 当MySqlConnector 遇到类型为char(36)/char(32)

的字段时,会转换成guid,转换不成功时就失败报错了。解决方案就是在连接串上设置guid的处理方式,参考

MySQL .NET Connection String Options

GuidFormat 一节。 其实只要GuidFormat不是char32,36都能避免这个错误。

同时github上的问题:

Sequential GUIDs

原文地址:https://www.cnblogs.com/jidanfan/p/14442052.html