0

I have the following table:

CREATE TABLE [dbo].[Table1](
    Table1ID [uniqueidentifier] NOT NULL,
    Name [varchar](200) NOT NULL,
    [Date] [smalldatetime] NOT NULL,
 CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED 
(
    [Table1ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],

) ON [PRIMARY]
GO

ALTER TABLE [dbo].[Table1] ADD  CONSTRAINT [DF_Table1_Table1ID]  DEFAULT (newid()) FOR [Table1ID]
GO

When I map it using Database first to EF 6.1.3 everything maps fine, generating an Entity Table1s with the Table1ID of type Guid.

When I insert a new item to the database using EF, I can't leave the Table1ID field empty, I have to do the following:

var newRecord = new Table1();
newRecord.Table1ID = Guid.NewGuid();
newRecord.Name = "Test"
newRecord.Date = DateTime.Now;

Manager.Entities.Table1s.Add(newRecord);
Manager.Entities.SaveChanges();

This is sent from a desktop application (WPF), and there will be multiple clients (the application will be in several PCs around the network, all connected to the same DB).

When I create a Guid.NewGuid(), the Guid is generated locally at the client side and then sent to the database when I do SaveChanges().

Doesn't this mean that at some point there is a change for the Guid to be duplicated? (Guid.NewGuid will generate a Guid that is already in the database).

If this could happen, is there a different way to generate a Guid from the database before creating the record?

I tried adding an empty Guid but that didn't help.

I found related questions like these:

But it seems like everyone uses applications that are developed in the Server (e.g. ASP.net Apps), which don't have issues as all the Guid.NewGuid() are generated in the server.

gbdavid
  • 1,639
  • 18
  • 40
  • 1
    Have you considered to make the Desktop Client query the SQL Server for a new GUID before creating the record? (You could run a simple SQL statement like `SELECT NEWID()` to get a new GUID generated by the SQL Server) – bassfader Oct 25 '17 at 12:15
  • On attempted save, check for a duplicate GUID. If it exists, create a new one instead before saving. The chances are beyond remote, however: https://stackoverflow.com/questions/39771/is-a-guid-unique-100-of-the-time – danielmcn Oct 25 '17 at 12:18

2 Answers2

1

Doesn't this mean that at some point there is a change for the Guid to be duplicated?

It could generate a duplicate guid but it is unlikely. The likelihood of getting a duplicate them is very low:

What are the chances to get a Guid.NewGuid () duplicate?

If this could happen, is there a different way to generate a Guid from the database before creating the record?

You could for example generate the identity in the database itself by using an auto incremented primary key:

How do I add a auto_increment primary key in SQL Server database?

But generating guids on the client side should also work just fine.

mm8
  • 163,881
  • 10
  • 57
  • 88
0

It could generate duplicate, but the chance is so small that you shouldn't care about it. On wiki is article about it https://en.wikipedia.org/wiki/Universally_unique_identifier#Collisions . "This number is equivalent to generating 1 billion UUIDs per second for about 85 years, and a file containing this many UUIDs, at 16 bytes per UUID, would be about 45 exabytes, many times larger than the largest databases currently in existence, which are on the order of hundreds of petabytes."

CrazyBaran
  • 572
  • 3
  • 20