-
Notifications
You must be signed in to change notification settings - Fork 5
Entity Settings
Metadata of each entity has to be defined, so that CherrySeed knows the entity and also knows which fields are primary keys, which fields are foreign keys which entity specific repositories should be used, etc.
First, you have to declare the entity via the method ForEntity<T>()
. With this setting CherrySeed knows that there is an entity of a specific type T in the test data. If you have multiple entities then you have multiple calls of ForEntity<T>()
.
Be careful, the order of the ForEntity<T>()
calls is relevant. The seeding order is the same order as the order of the ForEntity<T>()
settings. So if you have foreign keys the order is important.
var config = new CherrySeedConfiguration(cfg =>
{
...
// entity Address seeded before entity Person
cfg.ForEntity<Address>();
cfg.ForEntity<Person>();
});
If the entity has a primary key you have to declare it. You do this with the method WithPrimaryKey(Expression<Func<T, object>> primaryKeyExpression)
.
var config = new CherrySeedConfiguration(cfg =>
{
...
cfg.ForEntity<Person>()
.WithPrimaryKey(e => e.Identification);
});
To simplify this primary key setting CherrySeed uses some global conventions here. Fields with the name 'Id', 'ID', '<ClassName>Id' or '<ClassName>ID' are automatically declared as primary keys. So an entity 'Person' with primary key 'PersonId' doesn’t need a WithPrimaryKey(...)
call. You can also set some global custom conventions with method WithDefaultPrimaryKeyNames(params string[] primaryKeyNames)
.
var config = new CherrySeedConfiguration(cfg =>
{
...
cfg.WithDefaultPrimaryKeyNames("Identification", "Prefix_{ClassName}_ID");
...
});
If an entity has a reference to another entity then you have to declare a foreign key. This is done with the method WithReference(Expression<Func<T, object>> referenceExpression, Type referenceEntity)
. The first parameter declares a field as foreign key and the second parameter declares the type of the referenced entity.
var config = new CherrySeedConfiguration(cfg =>
{
...
cfg.ForEntity<Person>()
.WithReference(e => e.AddressId, typeof(Address));
});
Many O/R mapping frameworks support two ways to create references:
- Foreign Key Field: The foreign key field of entity A contains the primary key ID of the referenced entity B. Typical data types of foreign key fields are integer, Guid or string.
- Navigation Field: The navigation field of entity A contains a reference to the referenced entity B. The navigation field data type is the class of the referenced entity B.
CherrySeed supports both ways of creating references and will handle the referencing for you in the background.
It is possible to set a default value to a specific field of an entity. For example you want all test data items of entity Person
set the field IsApproved
to true
. So it is not necessary to set this field in the data provider for all items. Instead it is enough to set it up in the entity settings with the following methods:
-
WithFieldWithDefaultValue(Expression<Func<T, object>> fieldExpression, IDefaultValueProvider defaultValueProvider)
Setting the field of the entity with a lambda expression and pass an instance of a class which implement the interfaceIDefaultValueProvider
. -
WithFieldWithDefaultValue(Expression<Func<T, object>> fieldExpression, Func<object> defaultValueFunc)
Setting the field of the entity with a lambda expression and pass an functionFunc<object>
which return the default value.
In the following code block a default value of field IsAssigned
and Birthdate
is set:
var config = new CherrySeedConfiguration(cfg =>
{
...
cfg.ForEntity<Person>()
.WithFieldWithDefaultValue(e => e.IsAssigned, () => true)
.WithFieldWithDefaultValue(e => e.Birthdate, new RandomDateProvider());
});
If a field has a default value and is also set in the data provider then the default value is overruled.
You can use the method HasEntityName(string entityName)
to set a custom entity name for a specific entity. For more info see How to Have a Match?.
var config = new CherrySeedConfiguration(cfg =>
{
...
cfg.ForEntity<Person>()
.HasEntityName("MyCustomPerson");
});
It is possible to override the global repository for a specific entity. This is done with the method WithRepository(IRepository repository)
by passing an instance of the repository.
var config = new CherrySeedConfiguration(cfg =>
{
...
cfg.ForEntity<Person>()
.WithRepository(entitySpecificRepository);
});
It is also possible to overwrite the global ID generator for a specific entity. To do so one of these methods has to be called:
WithPrimaryKeyIdGenerationInDatabase()
WithPrimaryKeyIdGenerationInApplicationAsInteger(int startId = 1, int steps = 1)
WithPrimaryKeyIdGenerationInApplicationAsGuid()
WithCustomPrimaryKeyIdGenerationInApplication(IIdGenerator generator)
The following lines of code show how an integer primary key ID generator is set for a specific entity.
var config = new CherrySeedConfiguration(cfg =>
{
...
cfg.ForEntity<Person>()
.WithPrimaryKeyIdGenerationInApplicationAsInteger();
});
For more info on primary key generators and these different methods see Primary Key ID Generation.