从数据库中获取Guid [英] getting Guid from database

查看:269
本文介绍了从数据库中获取Guid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发生了一些奇怪的事情,我不再能够通过.net实体模型从我的mysql数据库中获取GUID类型.

Something strange happend, I am no longer able to get the GUID type from my mysql database with the .net entity model.

前一段时间,我创建了一个使用Guid作为ID的数据库.我使用实体框架创建了与此数据库的连接,并且一切正常.

A while ago i created a database which used Guid's as ID. I created a connection to this database with the entity framework and everything worked fine.

但是我不久前丢失了数据库,不得不重新创建它,但是现在,由实体框架模型创建的类具有一个字符串作为ID,而不是以前的GUID.

however i lost the database a while ago and i had to create it again, now however, the classes that get created by the entity framework model have a string as ID, not the GUID it used to be.

我需要什么类型的ID才能在mysql数据库中创建ID,因此我可以简单地执行以下操作:

What type do i need to make my ID's in my mysql database so I can simply do the following:

Image.ID = Guid.NewGuid();
EntityModel.Image.Add(Image);
EntityModel.SaveChanges();

这曾经很好用,现在(显然)我得到一个错误:无法将类型'System.Guid'隐式转换为'字符串'"

This used to work fine, now I (obviously) get an error: "Cannot implicitly convert type 'System.Guid' to 'string'"

我不记得我曾经做过的事情,并且想知道这里的人是否可以提供帮助. 我确实读过有关UUID()的文章,但这不是我做的. 某种程度上,我感觉到mysql已更新,某种程度上,它不再可行,但这对我来说似乎很奇怪.

I cannot remember how I once did it and am wondering if people here could help. I did read about UUID() but that's not how I did it. Somehow I've got the feeling mysql updated and somehow it's no longer possible but that seems odd to me.

我确实调查了一下,看到很多人建议在MySQL中将id设为二进制类型,然后将Guid转换为二进制格式并进行存储.我希望不要编写一个转换类来将Guid隐式转换为Binary ...

I did look into this and saw a lot of people suggesting making the id's a binary type in mysql and converting the Guid to it's binary form and storing that. I'm hoping not to write a conversion class to implicitly cast Guid to Binary...

推荐答案

如果您使用'Old Guids = true',则Binary(16)应该与较新的连接器一起使用,否则它想使用char(36)

Binary(16) should work with the newer connectors if you use 'Old Guids=true', otherwise it wants to use char(36)

这篇关于从数据库中获取Guid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆