Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it better to generate guid manually in my app or let sql server/azure sql generate it for me

How do I generate guid for my entities in my app that will be stored by entity framework to sql azure.

Right now I use Id = Guid.NewGuid(). Is this correct?

EDIT

To clarify why i'm asking this for, below is my explanations

My friend told me that sql server can generate guid with "NewID()". Right now i generate guid in my app using "Guid.NewGuid()". Which one should i use? For me, my current approach is more convenient because i get the guid value even before i save it to the database. What is the up/down side of each approach?

like image 670
Reynaldi Avatar asked Apr 20 '15 06:04

Reynaldi


People also ask

Is GUID () a good option for primary key and why?

GUIDs can be considered as global primary keys. Local primary keys are used to uniquely identify records within a table. On the other hand, GUIDs can be used to uniquely identify records across tables, databases, and servers.

How do I create a new GUID in SQL Server?

GUIDs can be added to any table. If the table you want to edit participates in replication or offline mapping or contains a GUID, you must insert a unique value to the global ID or GUID column when you insert a new record to the table using SQL. To do this, you can use the newid() function.

Which data type is used for assigning GUID value in SQL?

The globally unique identifier (GUID) data type in SQL Server is represented by the uniqueidentifier data type, which stores a 16-byte binary value. A GUID is a binary number, and its main use is as an identifier that must be unique in a network that has many computers at many sites.


1 Answers

It really doesn't matter where you're generating the Guid because it is guaranteed to be unique in either case. But people coming from a Domain Driven Design background will suggest that generating it on the C# side is the correct approach as you can use it right away without having to query the database if the need arrives to update a different entity with that Guid.

like image 158
Soham Dasgupta Avatar answered Oct 24 '22 00:10

Soham Dasgupta