EF Core DB First模式下脚手架同步时自定义类与属性名需求
Great question! Keeping domain models in sync with database schema changes via scaffolding while preserving custom name mappings is a common pain point—here's how to solve it cleanly and avoid losing your config every time you run the tool.
Most scaffolding tools (like EF Core's dotnet ef dbcontext scaffold) generate partial classes for your domain models and DbContext. This is your secret weapon: you can create matching partial class files in separate locations to hold your custom mapping rules, and these files won't be overwritten when you re-run scaffolding.
Example: Custom Property & Table Mapping with DataAnnotations
Suppose scaffolding generates a Company.cs file from the COMPANY_PROFILE table (with a default CompanyName property mapped to COMPANY_NAME):
// Auto-generated by scaffolding — DO NOT EDIT THIS FILE namespace YourProject.Domain.Entities; public partial class Company { public string CompanyName { get; set; } = null!; }
Create a separate file (e.g., Company.DomainMappings.cs) to define your custom renaming:
// Custom mapping config — THIS FILE WON'T BE OVERWRITTEN namespace YourProject.Domain.Entities; [Table("COMPANY_PROFILE")] // Maps domain class Company to DB table COMPANY_PROFILE public partial class Company { [Column("COMPANY_NAME")] // Maps WorkingCompany property to DB column COMPANY_NAME public string WorkingCompany { get; set; } = null!; }
Now, when you re-run scaffolding, the auto-generated Company.cs gets overwritten, but your custom mappings in the partial file stay intact.
If you prefer Fluent API over DataAnnotations, apply your mappings in a partial DbContext file. The scaffolded DbContext is also partial, so your custom logic won't be touched during re-scaffolding.
// Custom DbContext config — NOT auto-generated namespace YourProject.Data; public partial class YourDbContext : DbContext { protected override void OnModelCreating(ModelBuilder modelBuilder) { // First run the auto-generated OnModelCreating logic (if present) base.OnModelCreating(modelBuilder); // Define your custom mappings here modelBuilder.Entity<Company>() .ToTable("COMPANY_PROFILE") .Property(c => c.WorkingCompany) .HasColumnName("COMPANY_NAME"); } }
When you need to sync with new database schema changes, run your scaffolding command with the --force flag to overwrite auto-generated files—but since your custom config lives in separate partial files, it won't be lost. For EF Core, the command looks like this:
dotnet ef dbcontext scaffold "YourConnectionString" Microsoft.EntityFrameworkCore.SqlServer \ --context-dir Data \ --output-dir Domain/Entities \ --no-onconfiguring \ --force
--context-dir: Specify where to put the DbContext files--output-dir: Specify where to put domain entities--no-onconfiguring: Skip generating theOnConfiguringmethod (if you handle config elsewhere)--force: Overwrite existing auto-generated files
To save time, create a script (PowerShell or bash) with your scaffolding command and parameters. For example:
# PowerShell script: Scaffold-DbModels.ps1 dotnet ef dbcontext scaffold "Server=YOUR_SERVER;Database=YOUR_DB;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer ` --context-dir Data ` --output-dir Domain/Entities ` --no-onconfiguring ` --force
Just run the script whenever your database schema changes!
内容的提问来源于stack exchange,提问作者Ghini Antonio

