1 Star 0 Fork 2.5K

jiachunhui / Admin.NET

forked from zuohuaijun / Admin.NET 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
代码生成器使用.md 6.72 KB
一键复制 编辑 原始数据 按行查看 历史
wqj1991 提交于 2021-04-28 17:29 . 代码生成器文档

代码生成器使用教程

代码分析

关于实体列表的获取

  • 先看这段代码, Table 列表从 DbContext 的 Model 中获取的

    public List<TableOutput> GetTableList()
    {
        return Db.GetDbContext().Model.GetEntityTypes().Select(u => new TableOutput
        {
            TableName = u.GetDefaultTableName(),
            TableComment = u.GetComment()
        }).ToList();
    }
  • 接着我们看下 Furion 是怎么样定义实体的, 在源码 AppDbContextBuilder.cs

    重点看 只要继承 IEntityDependency 接口,都是实体 那部分代码, 所以只要继承了 IEntityDependency, 便可以在 list 中得到

    private static DbContextCorrelationType GetDbContextCorrelationType(DbContext dbContext, Type dbContextLocator)
    {
        // 读取缓存
        return DbContextLocatorCorrelationTypes.GetOrAdd(dbContextLocator, Function(dbContext, dbContextLocator));
        // 本地静态方法
        static DbContextCorrelationType Function(DbContext dbContext, Type dbContextLocator)
        {
            var result = new DbContextCorrelationType { DbContextLocator = dbContextLocator };
            // 获取当前数据库上下文关联类型
            var dbContextEntityCorrelationTypes = EntityCorrelationTypes.Where(u => IsInThisDbContext(dbContextLocator, u));
            // 组装对象
            foreach (var entityCorrelationType in dbContextEntityCorrelationTypes)
            {
                // 只要继承 IEntityDependency 接口,都是实体
                if (typeof(IPrivateEntity).IsAssignableFrom(entityCorrelationType))
                {
                    // 添加实体
                    result.EntityTypes.Add(entityCorrelationType);
    
                    // 添加无键实体
                    if (typeof(IPrivateEntityNotKey).IsAssignableFrom(entityCorrelationType))
                    {
                        result.EntityNoKeyTypes.Add(entityCorrelationType);
                    }
                }
    
                if (typeof(IPrivateModelBuilder).IsAssignableFrom(entityCorrelationType))
                {
                    // 添加模型构建器
                    // 添加全局筛选器
                    // 添加种子数据
                    // 添加动态表类型
                    // 添加实体数据改变监听
                }
            }
    
            return result;
        }
    }
  • 再看下 Furion 官方文档, 关于实体的定义, 9.3 数据库实体 | Furion (gitee.io)

    Furion 框架提供多种定义实体的接口依赖:

    • IEntity:实体基接口,是所有实体的基接口
    • IEntityNotKey:无键实体接口,也就是视图、存储过程、函数依赖接口
    • EntityBase:实体基抽象类,内置了 IdTenantId 字段
    • Entity:实体通用抽象类,继承自 EntityBase,同时内置 CreatedTimeUpdatedTimeIsDeleted 字段
    • EntityNotKey:无键实体抽象类,视图、存储过程、函数依赖抽象类
  • 上面提到的这些最上层都是继承了 IEntityDependency 的, 这些可以去源码查看, 这里我就不展示了

  • 所以, 我们定义实体只需要继承上面的任意一个, 就可以被扫描到了

具体操作

新增实体

  • Dilon.Application 的项目中, Entity文件夹下

  • 新建 CodeGenTest.cs

    /// <summary>
    /// 代码生成实体事例(EF)
    /// </summary>
    [Table("code_gen_test")]
    [Comment("代码生成事例")]
    public class CodeGenTest : DEntityBase
    {
        /// <summary>
        /// 名称
        /// </summary>
        [Comment("名称")]
        public string Name { get; set; }
    
        /// <summary>
        /// 昵称
        /// </summary>
        [Comment("昵称")]
        public string NickName { get; set; }
    
        /// <summary>
        /// 生日
        /// </summary>
        [Comment("生日")]
        public DateTimeOffset Birthday { get; set; }
    
        /// <summary>
        /// 年龄
        /// </summary>
        [Comment("年龄")]
        public int Age { get; set; }
    }
  • 新建 CodeGenTestChild.cs

    /// <summary>
    /// 代码生成事例子表
    /// </summary>
    [Table("code_gen_test_child")]
    [Comment("代码生成事例子表")]
    public class CodeGenTestChild : DEntityBase
    {
        [ForeignKey("CodeGenId")]
        public virtual CodeGenTest CodeGen { get; set; }
    
        /// <summary>
        /// 主表外键
        /// </summary>
        [Comment("主")]
        public virtual long CodeGenId { get; set; }
    
        /// <summary>
        /// 编码
        /// </summary>
        [Comment("编码")]
        [MaxLength(32)]
        public virtual string Code { get; set; }
    
        /// <summary>
        /// 名称
        /// </summary>
        [Comment("名称")]
        [MaxLength(32)]
        public virtual string Name { get; set; }
    }

生成migration并更新数据库

# vs
Add-Migration Add_CodeGenTest -Context DefaultDbContext
update-database -Context DefaultDbContext

# cmd, 在 backend 文件夹下执行
dotnet ef migrations add Add_CodeGenTest -c DefaultDbContext -p Dilon.Database.Migrations/Dilon.Database.Migrations.csproj -s Dilon.Web.Entry/Dilon.Web.Entry.csproj
dotnet ef database update -c DefaultDbContext -p Dilon.Database.Migrations/Dilon.Database.Migrations.csproj -s Dilon.Web.Entry/Dilon.Web.Entry.csproj

运行前后端

  • 在代码生成器页面, 新增

    image-20210428165930282

  • 生成子表, 和上面一样

  • 完成后的列表

    image-20210428170208018

  • 点击配置, 基础的配置不在展示, 这里主要说外键的配置

    image-20210428170313516

    image-20210428170538152

  • 保存, 点击列表中的生成

    image-20210428170737038

image-20210428170938653

  • 这是要重新启动后端, 重启后端, 重启后端

  • 这是业务就可以使用了

    image-20210428171445286

    image-20210428171628000

    image-20210428171710548

C#
1
https://gitee.com/aiyy/Admin.NET.git
git@gitee.com:aiyy/Admin.NET.git
aiyy
Admin.NET
Admin.NET
master

搜索帮助