This action will force synchronization from zuohuaijun/Admin.NET, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
先看这段代码, 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
:实体基抽象类,内置了 Id
,TenantId
字段Entity
:实体通用抽象类,继承自 EntityBase
,同时内置 CreatedTime
,UpdatedTime
,IsDeleted
字段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; }
}
# 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
在代码生成器页面, 新增
生成子表, 和上面一样
完成后的列表
点击配置, 基础的配置不在展示, 这里主要说外键的配置
保存, 点击列表中的生成
这是要重新启动后端, 重启后端, 重启后端
这是业务就可以使用了
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。