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                
#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                

GZY.EFCoreCompare

GZY.EFCoreCompare 是一个用于 对比数据库结构和 EF Core 代码中的 DbContext 的库。它基于 EF Core 的 Scaffolding 机制,生成 DatabaseModel(从数据库提取的模型),并与代码中的 DbContext 进行比对,从而找出两者之间的差异。

主要用途

  1. 检查数据库与代码是否匹配

    • 通过 EF Core Scaffolding 解析数据库结构(DatabaseModel)。
    • 读取代码中的 DbContext 及其 DbSet<TEntity> 实体定义。
    • 对比两者的字段、表、主键、索引等内容。
  2. 检测并生成数据库更新 SQL

    • 发现 数据库中有但代码中没有的表(可能需要删除)。
    • 发现 代码中有但数据库中缺失的表或字段(可能需要添加)。
    • 发现 数据类型、索引、约束等的差异(可能需要修改)。
  3. 帮助团队进行数据库变更管理

    • 在开发过程中,避免数据库和代码模型不同步
    • 适用于 数据库先行代码先行 的开发模式,确保两者一致。
    • 可用于 自动化 CI/CD 流水线,在部署前发现问题。
    • 可用于 部署/更新后判断数据库是否标准,在部署后发现客户或线上的问题。

工作流程

  1. 获取数据库结构
    使用 EF Core Scaffolding 解析数据库,并生成 DatabaseModel
  2. 读取代码中的 DbContext
    反射或解析代码中的 DbContext 及其实体。
  3. 比对两者的差异
    找出 表、字段、类型、索引、主键、外键 等方面的不同。
  4. 提供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的界面地址

  • 点击开始对比,进行比对

  • 效果如下图: image

  • 也可以点击导出Execl,导出比对的结果 image

Product 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.

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