0

I create manually my GUID for storing it to database like so

Guid EmailToken = Guid.NewGuid;

I tried to store it in an uniqueidentifier field of SQL with LINQ like

using (DBDataContext DB = new DBDataContext()) {
    Member M = new Member { ActEmlGuid = EmailToken };
    DB.Members.InsertOnSubmit(M);
    DB.SubmitChanges();
}

Unfortunately the SQL now has a different GUID than the one i created. Why is this happening? How can i overcome this? I do not want SQL to create the GUID for me.

And here are the properties of the SQL field

enter image description here

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
OrElse
  • 9,709
  • 39
  • 140
  • 253
  • 1
    I think there's a setting on the table to auto generate a value or not. change it. – i3arnon Jan 05 '14 at 15:20
  • @I3arnon I wish there was... At least i cannot find one to match :) – OrElse Jan 05 '14 at 15:50
  • not an answer to your dilema, but you could always just change the field type to char and then set the EmailToken.ToString() when saving... as i said -not potentially the answer you want, but would work!! – jim tollan Jan 05 '14 at 15:54
  • @jimtollan It sure will but i think that is a bad bad idea.. – OrElse Jan 05 '14 at 15:56
  • well, as you are not using the db engine to create the guid and are not using any of the other server based functionality on it, then it allows you to consider that as an alternative. at the end of the day, you have to ask your self *what purpuse do i wnat to achieve from maintaining the guid field type* and then work backwards. if it's just for creating a uniq id, then i personally feel that ToString() is perfectly acceptable, especially as the server plays no role in validating or scoping the input or output. – jim tollan Jan 05 '14 at 15:58
  • one other potential issue with the uniqidentifier match up is the fact that in your schema, it is nullable, this will obviously mean that you'll have to consider this as well. anyway, my cheap 2 cents said :-) good luck, will see how you reconcile – jim tollan Jan 05 '14 at 16:10
  • ok, this question bugged me, so sought a few answers that didn't compromise your schema. this one on SO looks like a good match: http://stackoverflow.com/questions/7182805/ef-4-1-codefirst-with-guid-not-generated-by-db-store-as-pk. ok, outta here now :-) – jim tollan Jan 05 '14 at 16:14
  • one final thought, try the attribute decorations mentioned in this SO topic: http://stackoverflow.com/questions/7174065/ef-returns-0000-0000-0000-xxx-as-guid-when-i-try-save-a-new-record-or-update-exi/7180770#7180770 – jim tollan Jan 05 '14 at 16:43
  • Isn't there a trigger on the table? This is pretty unique behavior, you know. – Gert Arnold Jan 05 '14 at 19:00

2 Answers2

0

ok chocol8 (grt name btw!!).

if you are using EF code first, you can add the following to your model on the guid.

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ActEmlGuid { get; set; }

This will enable sqlserver (server-side) guid generation, thus enabling you to use the correct field types as planned.

It's a tricky little nut this one, as so much depends on the mechanics of your interaction with the db, but hopefully the above will act as a guide in some way, otherwise, my nutty .ToString() option (mentioned in comments) may get you out of a tangle in the short-term -tho of course not advisable.

[edit] - if you are not using EF, then you can update your schema (in the properties page in your OP above) and add:

enter image description here

jim tollan
  • 22,305
  • 4
  • 49
  • 63
-1

Try this

Member M = new Member();
M.ActEmlGuid = Guid.NewGuid;
DB.Members.Add(M);
DB.SaveChanges();
  • benmed, the OP actally does this in a single hit already -> `Member M = new Member { ActEmlGuid = EmailToken };` – jim tollan Jan 05 '14 at 17:12