I am using below code to generate unique id value from C# to save in db primary column. DBs like SQL, Mongodb, AWS redhsift (on these dbs i have unique id column) Can I go forward or is there complication in future ?
var milliSeconds = DateTimeOffset.UtcNow.UtcTicks.ToString();
long id = long.Parse(milliSeconds)
Thanks
2条答案
按热度按时间r7knjye21#
You can create a singleton class for it which contains a private counter and a method to retrieve the counter. However, the retrieve method has a lock so it get executed one thread at a time. Since it's a singleton class, it will instantiate once when the program gets run and you can initialize your counter from the maximum value available for id in your database and after that it would get increased without duplication or interruption.
Here's some sample code in console:
ubof19bj2#
I am using another way to generate the unique bigint value it's works 99% but 1% is chance is that the project runs on multiple servers(load balancers)