DbTool
一个支持 DbFirst
、ModelFirst
和 CodeFirst
的数据库小工具。
DbFirst
是根据数据库中的表信息生成代码中的 Model,以及生成数据表结构文档
ModelFirst
是根据数据表信息或者数据表结构文档生成创建数据库的脚本
CodeFirst
是指根据 model 代码生成数据库表结构信息以及创建数据表的 SQL 脚本
在新的版本中主要增加对于一些新的 C# 特性的支持
-
全局引用
-
文件范围命名空间
-
可空引用类型
本次更新主要针对于 C# 的新特性做了一些支持
可空引用类型是 C# 8 开始引入的特性,启用之后默认引用类型也是不可为空的,比如说 string
是不可为 null
的,如果要允许为 null
则应声明为 string?
,而数据库中字段一般是会区分是否允许为 null
的,和这一特性就会比较契合,efcore 6 开始也会根据是否可以为空生成是否可为空的数据库列。
全局引用和文件范围命名空间是 C# 10 引入的新特性,不了解的小伙伴可以参考之前的介绍文章 C# 10 新特性 —— 命名空间的变化
DbFirst
以上数据表导出的代码示例:
namespace Models;
public class Notice
{
public Guid NoticeId { get; set; }
public string NoticeTitle { get; set; }
public string? NoticeDesc { get; set; }
public string? NoticeContent { get; set; }
public string? NoticePath { get; set; }
public string? NoticeExternalLink { get; set; }
public string? NoticeImagePath { get; set; }
public string? NoticeCustomPath { get; set; }
public int NoticeVisitCount { get; set; }
public DateTime UpdateTime { get; set; }
public string UpdateBy { get; set; }
public DateTime NoticePublishTime { get; set; }
public string NoticePublisher { get; set; }
public bool CheckStatus { get; set; }
public bool IsDeleted { get; set; }
}
允许为 null
的字段就会声明为可为空的引用类型如 string?
在导出方面,默认支持了导出 csv 文档
Model First
ModelFirst
ModelFirst 在原来的基础上集成了 Csv 文档的导入
Code First我们可以从一个 C# model 代码提取出其中的属性来生成数据库的表结构,生成不同数据库的创建表的脚本
CodeFirst
SettingsSettings
最后配置页面增加了针对新的选项的默认配置
More更多实现细节以及自定义可以查看和修改源码,你也可以根据需要自定义自己的插件,可以参考:https://github.com/WeihanLi/DbTool.Packages/blob/main/README.md
References-
https://github.com/WeihanLi/DbTool
-
https://github.com/WeihanLi/DbTool.Packages
-
数据库小工具 DbTool
-
.NET 6 中的隐式命名空间引用
-
C# 10 新特性 —— 命名空间的变化