Simple DB seeding for entities which have foreign keys.
Currently only supporting Sql Server(MsSql)
https://www.nuget.org/packages/Planto or
dotnet add package Planto --version 0.7.0
Seed entities in a database and automatically created related entities.
Create a Planto object and use the CreateEntity function. See below for custom data.
string TableName = "ExampleTable";
// recommended!
var plantoRandomValues = new Planto(_msSqlContainer.GetConnectionString(), DbmsType.MsSql,
options => options.SetValueGeneration(ValueGeneration.Random));
// only creates entities with default values, see Restriction part for more details
var plantoDefaultValues = new Planto(_msSqlContainer.GetConnectionString(), DbmsType.MsSql);
// if pk is not int, change e.g. to string or decimal
var id = await plantoRandomValues.CreateEntity<int>(TableName);
var id = await plantoDefaultValues.CreateEntity<int>(TableName);
- supports only tables with a single primary key
Set Schema if needed, else tables from all schemas are used
new Planto(ConnectionString, DbmsType,
options => options.SetDefaultSchema("myDbSchema"));
Right now the program can solve simple expressions
involving value comparisons using =
, <=
, >=
and like
To deactivate Check-Constraint value generation(currently experimental) do:
new Planto(ConnectionString, DbmsType,
options => options.SetColumnCheckValueGenerator(false));
Recommended
To identify which columns require special values based on a Check, you can use planto.AnalyzeColumnChecks(tableName).
This function returns the check constraints for the specified table and its columns.
From there, you can determine which data should likely be provided manually.
Create a class and use the TableName
and ColumnName
attribute to match the database naming with your class.
If no attribute is provided the class name and property name is used.
Properties not explicitly provided will be auto-generated using the configuration defined in options.
[TableName("TestTable")]
private class TestTableDb
{
[ColumnName("name")]
public string? MyName { get; set; }
[ColumnName("age")]
public int TestAge { get; set; }
}
You can add one or more objects to the function, and the library will automatically use the corresponding object for the related table. This is useful if, for example, you want to create a random Customer that is linked to another table where the generated entity requires a specific value.
await planto.CreateEntity<int>(TableName, new TestTable { name = myName, age = age});
- supports tables where id is managed by IDENTITY
- if a PK is not IDENTITY it is important to change SetValueGeneration to Random
new Planto(ConnectionString, DbmsType.MsSql,
options => options.SetValueGeneration(ValueGeneration.Random));
- add
MultipleActiveResultSets=true;
to your ConnectionString or setoptions.SetMaxDegreeOfParallelism(1)
- Coming soon
- improve multiple unique constraints for a table (mssql)
- Support special PKs for MsSql, like multiple PKs
- Logs
- DeSerialize
- NpgSql