Search code examples
c#sql-servercodefluent

Sequential GUID's in CodeFluent


We use Guid's as identifiers for at least half of our tables. For the other tables more 'natural' identifiers could be used so we could profit from a natural clustered index. We noticed that the usage of the Guids causes a lot of overhead in the database since they are not sequential.

Has anyone implemented Sequential Guids in Codefluent, or tried? Can you share your approach and/or code?


Solution

  • You can generate a new sequential guid in C# with UuidCreateSequential and assign it to the property in the constructor (OnAfterCreate rule).

    The CodeFluent Entities model:

    <cf:entity name="Customer">
      <cf:property name="Id" cfom:newGuidOnNew="false" key="true" />
      <cf:property name="Name" />
      <cf:rule typeName="OnAfterCreate" />
    </cf:entity>
    

    The custom code to generate a Sequential guid:

    partial class Customer
    {
        void OnAfterCreate()
        {
            Id = SequentialGuid.NewGuid();
        }
    }
    
    static class SequentialGuid
    {
        [DllImport("rpcrt4.dll", EntryPoint = "UuidCreateSequential")]
        public static extern Guid NewGuid();
    }