Hi @mo boy - Put simply, the NEWSEQUENTIALID (Transact-SQL) function is dependent upon a single Windows host, since being restarted, to maintain a consistent sequence as NEWSEQUENTIALID is a wrapper over the Windows UuidCreateSequential function, with some byte shuffling applied. This is the scenario with SQL Server installed on Windows Server in an on-premise environment. That SQL Server will sequentially generate GUIDs until it is restarted where it will then pick-up at another starting point and start generating a GUID sequence but in another block or GUID range.
The UuidCreateSequential function has hardware dependencies. On SQL Server, clusters of sequential values can develop when databases (such as contained databases) are moved to other computers. When using Always On and on SQL Database, clusters of sequential values can develop if the database fails over to a different computer.
With Azure SQL Database instances running as nodes in a clustered infrastructure, and without the pinning concept available like with VMs, the NEWSEQUENTIALID function will never maintain any sequential algorithm as you desire...however! I found an elegant solution by Anuj Chaudhary using NEWSEQUENTIALID() with Entity Framework and SQL Azure.
In his example, he is using Code First Migrations which places [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
on the GUID column in EF and will generate a database creation script with the following attribute on the GUUD column: c.Guid(nullable: false, identity: true)
.
His solution was to change this to: c.Guid(nullable: false, identity: true, defaultValueSql: "newsequentialid()")
which negates the inconsistency in the GUID sequence in a multi-host environment.
I have not attempted to reproduce this but can do so should you experience any issues with the information I have provided. Please let me know if you have any additional questions or if this resolves your issue.