Overview
When scaffolding Entity Framework Core models from an existing database, you may encounter databases that use naming conventions that don’t align with C# coding standards. For example, a PostgreSQL database might have columns namedNIIN, FSC, INC, while C# conventions prefer Niin, Fsc, Inc.
The Property Name Overrides feature in EasyAF.EFCoreToEdmx allows you to define custom mappings between database column names and CLR property names while maintaining accurate EDMX generation.
Features
- Custom Property Naming: Map database column names to C# property names on a per-entity basis
- Automatic HasColumnName Generation: Injects
HasColumnName()calls inOnModelCreating - IgnoreTrackingFields Support: Automatically adds
IgnoreTrackingFields()for all entities - Self-Reference Fixes: Improves naming for self-referential relationships (InverseParent → Children)
Configuration
Add apropertyNameOverrides section to your .edmx.config file:
Configuration Structure
- Outer Dictionary Key: Entity name (after pluralization, e.g., “NationalStockNumber” not “NationalStockNumbers”)
- Inner Dictionary:
- Key: Database column name (e.g., “NIIN”)
- Value: Desired CLR property name (e.g., “Niin”)
How It Works
1. Database Scaffolding
When scaffolding from your database, EF Core generates entities with properties that match C# naming conventions:2. OnModelCreating Enhancement
The scaffolder enhances theOnModelCreating method with:
Before Enhancement:
3. EDMX Generation
The EDMX file correctly maps:- SSDL (Storage): Uses actual database column names (NIIN, FSC)
- CSDL (Conceptual): Uses CLR property names (Niin, Fsc)
- MSL (Mapping): Maps between storage and conceptual names
Benefits
1. Database Accuracy
MaintainsUseDatabaseNames = true for accurate database column representation while allowing custom CLR naming.
2. Clean Code Generation
Generated entities follow C# naming conventions while preserving database column mappings.3. Tracking Fields Support
Automatically addsIgnoreTrackingFields() for entities that implement tracking interfaces like ICreatedAuditable or IUpdatedAuditable.
4. Backward Compatibility
Existing configurations withoutpropertyNameOverrides continue to work unchanged.
Common Scenarios
PostgreSQL with Uppercase Columns
Legacy Database with Abbreviations
Mixed Naming Conventions
Best Practices
- Entity Names: Use the singular entity name in configuration, not the pluralized table name
- Consistency: Apply consistent naming patterns across related entities
- Documentation: Document why specific overrides are needed for future developers
- Testing: Verify generated EDMX files map correctly to your database schema
Troubleshooting
HasColumnName Not Generated
- Verify the entity name matches exactly (case-sensitive)
- Check that the database column name in the config matches the actual database
- Ensure
UseDatabaseNamesis set totrue(default)
IgnoreTrackingFields Not Added
- Confirm you’re using the latest version of EasyAF.EFCoreToEdmx
- Check console output during scaffolding for any enhancement errors
Self-References Still Show InverseParent
- The enhancement automatically fixes this, but verify no custom navigation properties override the fix
Example: Complete Configuration
- Scaffold only the specified tables
- Apply custom property names for NationalStockNumber and Part entities
- Generate HasColumnName() calls for mapped properties
- Add IgnoreTrackingFields() to all entities
- Fix any self-referential relationships