DateTime.Now.Ticks repeating inside a loop

前端 未结 6 1174
小鲜肉
小鲜肉 2021-01-27 02:55

I am trying to generate a unique ID for the primary key of a table and I am using DateTime.Now.Ticks for it. That\'s a requirement for now we can\'t use Ident

6条回答
  •  日久生厌
    2021-01-27 03:26

    Using DateTime.Now.Ticks as database identity is a very bad idea. Even you resolved the "repeating issue" in your question, your application will probably break in the future, for example, a very common scenario, the application is deployed on multiple servers.

    You can either (1) use a database auto-generated, auto-increased id, or (2) use Guid to fix the issue.

    EDIT Under the database performance consideration, auto-increased long id has a better performance than guid in most circumstances.

    EDIT Treat tick as an unit of time measurement just like year/month/day/hour/minute/second/millisecond/nanosecond. Tick is between millisecond and nanosecond, 1 millisecond = 100000 ticks.

提交回复
热议问题