Skip to content

Commit e4fa276

Browse files
authored
Enable custom naming for table names (#687)
### Summary & Motivation Enable custom naming for table names as a follow-up to the **"Simplify DbContext by extracting aggregate configuration into dedicated classes"** change from a few pull requests ago. This allows more flexibility in defining table names instead of relying solely on convention-based pluralization. ### Checklist - [x] I have added tests, or done manual regression tests - [x] I have updated the documentation, if necessary
2 parents e158dc3 + 3c55f7d commit e4fa276

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

application/shared-kernel/SharedKernel/EntityFramework/SharedKernelDbContext.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,16 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
3030
// Set pluralized table names for all aggregates
3131
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
3232
{
33-
var tableName = entityType.GetTableName()!.Pluralize();
34-
entityType.SetTableName(tableName);
33+
var tableNameAnnotation = entityType.GetAnnotations().FirstOrDefault(a => a.Name == "Relational:TableName");
34+
if (tableNameAnnotation?.Value is not null)
35+
{
36+
entityType.SetTableName(tableNameAnnotation.Value.ToString());
37+
}
38+
else
39+
{
40+
var tableName = entityType.GetTableName()!.Pluralize();
41+
entityType.SetTableName(tableName);
42+
}
3543
}
3644

3745
// Ensures that all enum properties are stored as strings in the database.

0 commit comments

Comments
 (0)