Using Guid as Id column in NHibernate causes format exception when using MySQL

对着背影说爱祢 提交于 2019-12-04 23:43:32

问题


When I define the NHibernate entity/mapping to use Guid as identity column I receive an exception. The Guid column is generated as a varchar(40), but the content seem to be binary.

Is there a solution to this? For now I'm just using plain ol' int, but it would be nice to know for future projects! :)


回答1:


MySql Connector documentation states that from version 5.2 of .NET connector they treat GUID's as BINARY(16) not VARCHAR(40).

Since current MySQL dialect in nhibernate doesn't updated to reflect this change (actually an issue is prepared) you need to manually convert these fields to BINARY(16) after nhibernate generate the schema.




回答2:


Another update to this is that the latest connectors use Char(36)

This option was introduced in Connector/NET 6.1.1. The backend representation of a GUID type was changed from BINARY(16) to CHAR(36). This was done to allow developers to use the server function UUID() to populate a GUID table - UUID() generates a 36-character string. Developers of older applications can add 'Old Guids=true' to the connection string in order to use a GUID of data type BINARY(16).



来源:https://stackoverflow.com/questions/1034879/using-guid-as-id-column-in-nhibernate-causes-format-exception-when-using-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!