Unique ID for database tables in .NET
Can someone list widely used methods for generating unique IDs to be used in DB tables (PK or otherwise)
Should the table field be string always (for flexibility) or GUID (tight coupling) or simply use IDENTITY always ?
If it is string then, of course i have option to change generator logic anytime.
With IDENTITY i feel constrained because i have to depend on database. But is it good option for fields to publish outside like an Invoice #.
For small size of data say 10000 records, what option i have to get smallest length unique but random alpha-numeric ?
I face many situations where i need such unique number for various purposes, if i am unable to think through a good option, i have to fallback to good old and q开发者_JS百科uick .NET Guid.newGuid.ToString().
But i need to hear from experienced folks, What are the general guidelines and things to watch out for ?
You can simply use the below in order to create a unique id
Guid.NewGuid();
personally i prefer to set the table field to unique identifier.
Always use the smallest ID possible.
If you do not need IDs generated in the business tier (e.g. many clients saving on round trip), use an IDENTITY column in your table, else use a GUID, but do not place your clustered index on the GUID column unless it's also a COMB GUID.
精彩评论