Fork me on GitHub

Custom Identity Strategies Edit on GitHub


A custom ID generator strategy should implement IIdGeneration.


public class CustomdIdGeneration : IIdGeneration
{
    public IEnumerable<Type> KeyTypes { get; } = new Type[] {typeof(string)};

    public IIdGenerator<T> Build<T>()
    {
        return (IIdGenerator<T>) new CustomIdGenerator();
    }

    public bool RequiresSequences { get; } = false;

    public class CustomIdGenerator : IIdGenerator<string>
    {
        public string Assign(ITenant tenant, string existing, out bool assigned)
        {
            assigned = true;
            return "newId";
        }
    }
}

The Build() method should return the actual IdGenerator<T> for the document type, where T is the type of the Id field.

For more advances examples you can have a look at existing ID generator: HiloIdGeneration, CombGuidGenerator and the IdentityKeyGeneration,

To use custom id generation you should enabled it when configuring the document store. This defines that the strategy will be used for all the documents types.


options.DefaultIdStrategy = (mapping, storeOptions) => new CustomdIdGeneration();

It is also possible define a custom id generation algorithm for a specific document type.


options.Schema.For<UserWithString>().IdStrategy(new CustomdIdGeneration());