I have this table:
CREATE TABLE [dbo].[SandTable](
[Id] [uniqueidentifier] NOT NULL,
[Date] [date] NULL,
CONSTRAINT [PK_SandTable] PRIMARY KEY)
ALTER TABLE [dbo].[SandTable] ADD CONSTRAINT [DF_SandTable_Id] DEFAULT (NEWID()) FOR [Id]
Question is not about using NEWID() vs NEWSEQUENTIALID().
I use linqPad to test the table.
SandTables.InsertOnSubmit(new SandTable
{
// I don't provide any value for Id
Date = DateTime.Now
});
SubmitChanges();
My initial idea was to create an Id column that is able to initialize itself to a value when no Id is provided but will use the id provided when one is provided.
But because Guid is a struct, not a class the Id is never null, Id is initialized to his default value (00000000-0000-0000-0000-000000000000
). So SQL server consider that Id has always a value and then the NEWID() default instruction is never called.
Is it possible to force the call to NEWID() on specific value? Should I use a trigger to evaluate the value of Id and when it's (00000000-0000-0000-0000-000000000000
) then call NEWID()? What are the solutions or workaround?