Skip to content

Primary Key ID Generation

Michael Altmann edited this page Sep 21, 2016 · 11 revisions

In the test data human-readable primary key IDs are used. During the seeding process these human-readable primary key IDs are not inserted into the database. Instead CherrySeed uses a built-in logic to set primary keys.

Commonly O/R mapping frameworks supports two ways to generate primary key IDs:

  • ID generation in database (Database is responsible to generate valid primary key IDs)
  • ID generation in application (Application is responsible to generate valid primary key IDs)

Both ways are supported in CherrySeed, so it is up to you to choose one.

Existing ID Generators

ID Generation in Database

The database generates primary key IDs. This is globally the default behavior.

ID Generation in Application

CherrySeed replaces the human-readable IDs which are defined in the test data with auto-generated primary key IDs. The following ID generators are supported:

  • Integer ID generator: WithPrimaryKeyIdGenerationInApplicationAsInteger(int startId = 1, int steps = 1)
  • Guid ID generator: WithPrimaryKeyIdGenerationInApplicationAsGuid()
  • String ID generator: WithPrimaryKeyIdGenerationInApplicationAsString(string prefix = "", int startId = 1, int steps = 1)

Custom ID Generators

If you need another ID generator you can create a custom one. For more info see Custom Primary Key ID Generator. A custom ID generator is set globally or entity specific with method WithCustomPrimaryKeyIdGenerationInApplication(IIdGenerator generator).

Setting Global ID Generator

A global primary key ID generator is set with the methods described above. For example, to set an integer ID generator globally you have to write these lines of code:

var config = new CherrySeedConfiguration(cfg =>
{
    ...
    cfg.WithPrimaryKeyIdGenerationInApplicationAsInteger();
});

Setting Entity Specific ID Generator

To override the global ID generator for a specific entity, see Setting Entity Specific Primary Key ID Generator

Clone this wiki locally