GZY.EFCoreCompare.Dm
8.0.1
dotnet add package GZY.EFCoreCompare.Dm --version 8.0.1
NuGet\Install-Package GZY.EFCoreCompare.Dm -Version 8.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GZY.EFCoreCompare.Dm" Version="8.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add GZY.EFCoreCompare.Dm --version 8.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: GZY.EFCoreCompare.Dm, 8.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install GZY.EFCoreCompare.Dm as a Cake Addin #addin nuget:?package=GZY.EFCoreCompare.Dm&version=8.0.1 // Install GZY.EFCoreCompare.Dm as a Cake Tool #tool nuget:?package=GZY.EFCoreCompare.Dm&version=8.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
GZY.EFCoreCompare
GZY.EFCoreCompare 是一个用于 对比数据库结构和 EF Core 代码中的 DbContext 的库。它基于 EF Core 的 Scaffolding 机制,生成 DatabaseModel
(从数据库提取的模型),并与代码中的 DbContext
进行比对,从而找出两者之间的差异。
主要用途
检查数据库与代码是否匹配
- 通过 EF Core Scaffolding 解析数据库结构(
DatabaseModel
)。 - 读取代码中的
DbContext
及其DbSet<TEntity>
实体定义。 - 对比两者的字段、表、主键、索引等内容。
- 通过 EF Core Scaffolding 解析数据库结构(
检测并生成数据库更新 SQL
- 发现 数据库中有但代码中没有的表(可能需要删除)。
- 发现 代码中有但数据库中缺失的表或字段(可能需要添加)。
- 发现 数据类型、索引、约束等的差异(可能需要修改)。
帮助团队进行数据库变更管理
- 在开发过程中,避免数据库和代码模型不同步。
- 适用于 数据库先行 和 代码先行 的开发模式,确保两者一致。
- 可用于 自动化 CI/CD 流水线,在部署前发现问题。
- 可用于 部署/更新后判断数据库是否标准,在部署后发现客户或线上的问题。
工作流程
- 获取数据库结构
使用 EF Core Scaffolding 解析数据库,并生成DatabaseModel
。 - 读取代码中的
DbContext
反射或解析代码中的DbContext
及其实体。 - 比对两者的差异
找出 表、字段、类型、索引、主键、外键 等方面的不同。 - 提供UI查看界面与Execl报告
- 提供 UI查看界面,用于图形化查看差异。
- 生成 差异报告,供开发人员参考。
使用方式
1. 直接使用 GZY.EFCoreCompare.Core 进行比对
直接在项目中通过Nuget引用GZY.EFCoreCompare.Core
与对应数据库的驱动库,例:GZY.EFCoreCompare.MySql
库中,比对代码如下:
var compareEFcore= new CompareEFCore();
compareEFcore.CompareEfWithDbAsync(YourDbContext);
2. 集成图形化UI到项目中,进行持久化比对
- 在Web(MVC Raroz)或WebApi项目中通过Nuget引用
GZY.EFCoreCompare.UI
与对应的数据库驱动,例如:GZY.EFCoreCompare.MySql
- 在 Program或Startup中添加如下代码:
builder.Services.AddDbContext<TestDbContext>(options =>
{
string connStr = "";
options.UseMySql(connStr, new MySqlServerVersion(new Version(8, 0, 20)), builder => {
builder.SchemaBehavior(MySqlSchemaBehavior.Ignore);
});
}); //注入EFDbContext
builder.Services.AddEFCoreCompareUI(new CompareEFCoreConfig { CaseComparer=StringComparer.CurrentCultureIgnoreCase}); //需放在AddDbContext之后
app.UseEFCoreCompareUI(); //添加UI界面
在游览器输入地址:
http://localhost:5130/DBCompareUI
,/DBCompareUI
为UI的界面地址点击开始对比,进行比对
效果如下图:
也可以点击导出Execl,导出比对的结果
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- DM.Microsoft.EntityFrameworkCore (>= 8.0.0.28188)
- GZY.EFCoreCompare.Core (>= 8.0.1)
-
net9.0
- DM.Microsoft.EntityFrameworkCore (>= 8.0.0.28188)
- GZY.EFCoreCompare.Core (>= 8.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
8.0.1 | 67 | 2/11/2025 |