Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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! :)

like image 368
l3dx Avatar asked Jun 23 '09 20:06

l3dx


2 Answers

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.

like image 99
Ahmet Recep Navruz Avatar answered Sep 23 '22 19:09

Ahmet Recep Navruz


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).

like image 34
cdmdotnet Avatar answered Sep 23 '22 19:09

cdmdotnet