-
Notifications
You must be signed in to change notification settings - Fork 5
Primary Key ID Generation
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.
The database generates primary key IDs. This is globally the default behavior.
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)
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).
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();
});
To override the global ID generator for a specific entity, see Setting Entity Specific Primary Key ID Generator