0

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

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.

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.

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();

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

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.

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

Vincent
  • 1,497
  • 1
  • 21
  • 44

1 Answers1

2

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

Robert McKee
  • 21,305
  • 1
  • 43
  • 57
  • O really? xD if i wanted to do that i'd have done it, but i don't. if i do that it would mean i would first have to create strings from my guid for every single statement.. It used to work without .tostring() but probably the worst thing is creating guids from my strings... – Vincent May 29 '15 at 21:10
  • Sorry, your question is pretty confusing. Try setting the field as char(36) or binary(16) and make sure your connection string has "Old Guids=true" in it. Is that what you were looking for? – Robert McKee May 29 '15 at 21:18
  • Old GUIDS, i think that's what i'm looking for, let me test, sorry for the confusing question. – Vincent May 29 '15 at 21:19
  • Binary(16) should work with the newer connectors if you use 'Old Guids=true', otherwise it wants to use char(36) I think. Read more about it here: http://dev.mysql.com/doc/connector-net/en/connector-net-connection-options.html – Robert McKee May 29 '15 at 21:22
  • You are amazing, I had set the Old Guids=true but the varchar(36) was still a string, it had to be Binary(16). if you change your answer i will accept it :) – Vincent May 29 '15 at 21:27