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?
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With