I'm currently trying to add a unique Guid property on row insert in SQL Server. I am doing this via Entity Framework 6. To add the column to the table, I used the following query:
ALTER TABLE dbo.Reservation ADD ConfirmationID UNIQUEIDENTIFIER DEFAULT (NEWID()) WITH VALUES
Whenever I create a new row in this table, the NEWID()
is always set to 00000000-0000-0000-0000-000000000000
. I have tried to create a new Guid for the property in C# before my insert, however this returns the same result.
Here is the C# code I am calling as part of the insert function:
public ReservationClientModel Create(ReservationClientModel clientModel)
{
var reservation = new Reservation();
reservation = Mapper.Map<ReservationClientModel, Reservation>(clientModel, reservation);
reservation.ConfirmationID = new Guid();
_repository.Add(reservation);
_context.Commit();
return Mapper.Map<ReservationClientModel>(reservation);
}
Here is the code generated from EF as seen from EF Profiler:
INSERT [dbo].[Reservation]
([RoomID],
.....
.....
[ConfirmationID])
VALUES (15 /* @0 - [ID] */,
....
....
'00000000-0000-0000-0000-000000000000' /* @13 - [ConfirmationID] */)
Is there any way I can set this property to add a NEWID()
on insert, without setting the property in C# before hand?