You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:如何使Core 2领域模型与数据库变更保持同步?

Keeping EF Core 2.0 Models Synced with Ongoing Database Changes

Hey there! Since you're new to .NET Core 2.0, VS 2017, and MVC, let's walk through practical, straightforward ways to keep your scaffolded EF Core models in lockstep with the ongoing database changes your team is making—no scattered, confusing hacks required.

1. Full Sync with Force Override (Best for Small Changes + No Custom Model Code)

If you haven't added any custom logic directly to your auto-generated model classes, the simplest way to sync everything (updated existing tables + brand-new tables) is to re-run the Scaffold-DbContext command with the -Force flag. This will overwrite existing model files to match database updates and add new models for any tables that were recently created.

Here's the command (swap in your actual connection string and adjust the database provider if you're not using SQL Server):

Scaffold-DbContext "YourDatabaseConnectionString" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force

Heads up: This will wipe any custom code you've added directly to the scaffolded model files. If you have customizations, make sure to use tip #3 before running this.

2. Incremental Sync for New Tables Only

If the database team only added new tables (no changes to existing table structures), you don't need to overwrite all your models. Use the -Tables flag to target just the new tables:

Scaffold-DbContext "YourDatabaseConnectionString" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Tables NewTable1, NewTable2

This will generate model classes for the specified new tables without touching your existing, already-synced models.

3. Protect Your Custom Code

If you've added data annotations, custom properties, or helper methods to your model classes, never edit the auto-generated files directly—use partial classes instead.

For example, if you have a scaffolded Customer.cs file, create a new file named Customer.Custom.cs with this structure:

public partial class Customer
{
    // Your custom code lives here—this won't be overwritten when you re-scaffold
    public string FullName => $"{FirstName} {LastName}";
}

Now when you re-run the scaffold command with -Force, all your custom logic stays safe and intact.

4. Verify Changes First (Avoid Accidents)

Before syncing, use Visual Studio 2017's built-in Schema Compare tool to see exactly what changed in the database:

  1. Go to Tools > SQL Server > New Schema Comparison
  2. Set your source as the live database and target as your local model's schema
  3. Review the difference list to decide if you need a full sync or just incremental changes

This helps you avoid overwriting anything accidentally and ensures you only sync the changes that matter.


内容的提问来源于stack exchange,提问作者CJohn Ransom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:25:50