0

I have a (legacy) table with two guid fields. The second guid field can be null, but I want to auto-generate a non-dupe guid on insert using linq to sql. I have auto-sync set to "OnInsert" and Auto Generate Value set to True. But for some reason, it is not autogenerating/inserting guid values when the records are being put in the DB. I guess I could loop generating guids until I find one that is not in the DB and then add it in, but that does not seem very elegant. I am sure there is a better way to do this using linq to sql. Any suggestions on how to do this? Is it not making the guid because the nullable property is set to T?

enter image description here

I had a similar issue before, but learned how to change the properties of the ID field so that linq to sql could add the guid How do i prevent Linq from adding a GUID so that SQL server can do it? I can't use that solution here because the guid in this case is nullable and is not the PK, so I can't change the properties the way I did in the linked question.

Community
  • 1
  • 1
bernie2436
  • 22,841
  • 49
  • 151
  • 244
  • 2
    *"I guess I could loop generating guids until I find one that is not in the DB..."* If you use `Guid.NewGuid()` to generate GUIDs, you should never generate two that are identical. If that happens, you will have just beaten absolutely incredible odds. It is safe to assume that `Guid.NewGuid()` will return a GUID that nobody on the planet has ever generated before. – cdhowie Aug 01 '12 at 19:52
  • @cdhowie you should make that an answer so i can accept – bernie2436 Aug 02 '12 at 04:39

1 Answers1

2

I guess I could loop generating guids until I find one that is not in the DB...

If you use Guid.NewGuid() to generate GUIDs, you should never generate two that are identical. If that happens, you will have just beaten absolutely incredible odds. It is safe to assume that Guid.NewGuid() will return a GUID that nobody on the planet has ever generated before.

cdhowie
  • 158,093
  • 24
  • 286
  • 300