EntitiesFromEFCore.tt
1. Generate EF Core Entities
Generate EF Core entities from your database using scaffold command in your EDI command line or ordinary command line accessing dotnet.exe.
dotnet tool install --global dotnet-ef --version 6.0
dotnet ef dbcontext scaffold "Server=localhost;Database=d3orm_sample;Uid=d3orm;Pwd=***;" Pomelo.EntityFrameworkCore.MySql --startup-project ./KLO128.D3ORM.Sample.Infra.EFCore.MySQL.Gen --project ./KLO128.D3ORM.Sample.Infra.EFCore.MySQL.Gen -o ../KLO128.D3ORM.Sample.Infra.EFCore.MySQL/Db -f --namespace KLO128.D3ORM.Sample.Infra.EFCore.MySQL.Db -f
2. Update the Input Arguments
Update the paths and namespace names at the top of the text template script according to your project.
var targetPath = Host.ResolvePath("../../../KLO128.D3ORM.Website.Domain/Models/Entities/");
PropIdFormat = "{0}Id";
DefaultNameSpace = "KLO128.D3ORM.Website.Domain.Models.Entities";
DatabaseContextNames = new string [] {"d3orm_websiteContext"};
var targetPath = Host.ResolvePath("../../../KLO128.D3ORM.Sample.Domain/Models/Entities/");
var entitiesJsonPath = Host.ResolvePath("EntitiesFromEFCore.tt").Replace(".tt", ".json");
PropIdFormat = "{0}Id";
DefaultNameSpace = "KLO128.D3ORM.Sample.Domain.Models.Entities";
DatabaseContextNames = new string [] {"D3ORMSampleContext"};
DefaultPropValues = new Dictionary<string, string> { { "LastChange", "DateTime.UtcNow" } };
- targetPath is the path of the directory where the generated entities will be created or overwritten.
- entitiesJsonPath is the path to the configuration of the entities, where you define aggregations. If it is empty or not found both side aggregations (navigation properties) are included.
- PropIdFormat is the id property name format.
- DefaultNameSpace is the name of the namespace that will be used for each entity. It should correspond with the target folder path.
- DatabaseContextNames is an array of the local class/interface names without .cs suffix which should be excluded from the generation - typically EF Core DbContext.
- DefaultPropValues is for defining default property values. As you can see in the example, it is useful for datetimes.
3. Modify EntitiesFromEFCore.json
Modify EntitiesFromEFCore.json to reflect your entity aggregations. If the file does not exist, all both side navigation properties are persisted.
- D3ORM supports both side aggregations - each parent-child couple can have both navigation properties at the time (one to many and also "many to one"). D3ORM fills only one of them (avoiding the cyclic reference) with data according to a query.
- In case of having 2 or more references to the same entity type, use "Prop": "PropertyName", up to "Entity": "EntityName" to include only the Property named "PropertyName" or omit "Prop" to include all. See the example...
// JSON Array (EntitiesFromEFCore.json Example)
[
{
"Entity": "TourSerie", // Aggregate Root: Only aggregate roots are direct children of the JSON array.
"Aggs": [
{
"Entity": "Tournament", // Aggregate Child
"Aggs": "*" // This child is also an aggregate root, so '*' means include all the aggregates of the child.
}
]
},
{
"Entity": "Tournament", // Aggregate Root: Only aggregate roots are direct children of the JSON array.
"Aggs": [
{
"Entity": "TourSerie", // You can see both side aggregation in here.
"Aggs": "*"
},
{
"Entity": "TournamentTeam", // This is not an aggregate root
"Aggs": [
{
"Entity": "TournamentTeamStat", // This is also an aggregate root, because of "Aggs": "*"
"Aggs": "*"
},
{
"Entity": "Team", // This is also an aggregate root, because of "Aggs": "*"
"Aggs": "*"
},
{
"Entity": "Tournament", // This is also an aggregate root, because of "Aggs": "*" and it is both side aggregation with TournamentTeam.
"Aggs": "*"
}
]
},
{
"Entity": "Match", // This is also an aggregate root, because of "Aggs": "*"
"Aggs": "*"
},
{
"Entity": "Address", // This is not an aggregate root
"Aggs": []
}
]
},
{
"Entity": "Match",
"Aggs": [
{
//uncoment to include HomeTeam only
// "Prop": "HomeTeam",
"Entity": "Team",
"Aggs": "*"
},
{
"Entity": "MatchSetScore",
"Aggs": []
},
{
"Entity": "Tournament",
"Aggs": "*"
},
{
"Entity": "PlayoffRoundCouple",
"Aggs": "*"
}
]
},
...
]
4. Run the Text Template Script
Now you are ready to run the script.