Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DynamicEntity.DynamicCompileBuilder增加TypeBuilder只读属性和仅构建类型的方法 #1740

Merged
merged 2 commits into from
Mar 4, 2024

Conversation

vsuyi
Copy link
Contributor

@vsuyi vsuyi commented Mar 4, 2024

解决动态实体创建时需要引用自身类型或存在交叉引用时无法使用正在创建的实体类问题。

测试创建一个多级区域的Area实体:

var fsql = new FreeSqlBuilder()
    .UseConnectionString(DataType.Sqlite, "Data Source=|DataDirectory|\\test.db; Pooling=true;Min Pool Size=1")
    .Build();

// 动态构建实体类型,树形结构,引用自身类型
var areaBuilder = fsql.CodeFirst.DynamicEntity("Area", new TableAttribute { Name = "dy_area" });

areaBuilder.Property("id", typeof(int), new ColumnAttribute { IsPrimary = true })
    .Property("parentId", typeof(int?))
    .Property("name", typeof(string), new ColumnAttribute { StringLength = 30 });

// builder.TypeBuilder可作为类型被引用
areaBuilder.Property("parent", areaBuilder.TypeBuilder, new NavigateAttribute { Bind = "parentId" })
    .Property("children", typeof(List<>).MakeGenericType(areaBuilder.TypeBuilder), new NavigateAttribute { Bind = "parentId" });

var table = areaBuilder.Build();

// 迁移
fsql.CodeFirst.SyncStructure(table.Type);

测试创建交叉引用的两个类型,其中Channel可能属于某ChannelGroup(多对一),ChannelGroup包含多个Channel(一对多):

// 交叉引用类型,先定义两个类型,再Build
var channelBuilder = fsql.CodeFirst.DynamicEntity("Channel", new TableAttribute { Name = "dm_channel" });
var channelGroupBuilder = fsql.CodeFirst.DynamicEntity("ChannelGroup", new TableAttribute { Name = "dm_channel_group" });

channelBuilder.Property("id", typeof(long), new ColumnAttribute { IsPrimary = true })
    .Property("name", typeof(string), new ColumnAttribute { StringLength = 30 }).Property("channelGroupId", typeof(long?), new ColumnAttribute { IsNullable = true })
    .Property("channelGroupId", typeof(long?))
    .Property("channelGroup", channelGroupBuilder.TypeBuilder, new NavigateAttribute { Bind = "channelGroupId" });

channelGroupBuilder.Property("id", typeof(long), new ColumnAttribute { IsPrimary = true })
    .Property("name", typeof(string), new ColumnAttribute { StringLength = 30 })
    .Property("channels", typeof(List<>).MakeGenericType(channelBuilder.TypeBuilder), new NavigateAttribute { Bind = "channelGroupId" });

// Build时不能立即获取TableInfo,因为类型尚未真正构建
var channelEntityType = channelBuilder.BuildJustType();
var channelGroupEntityType = channelGroupBuilder.BuildJustType();

// 构建后才根据实体类型获取表信息
var channelTable = fsql.CodeFirst.GetTableByEntity(channelEntityType);
var channelGroupTable = fsql.CodeFirst.GetTableByEntity(channelGroupEntityType);

// 迁移
fsql.CodeFirst.SyncStructure(channelEntityType, channelGroupEntityType);

@2881099 2881099 merged commit 6c10057 into dotnetcore:master Mar 4, 2024
1 check passed
2881099 added a commit that referenced this pull request Mar 14, 2024
2881099 added a commit that referenced this pull request Mar 26, 2024
2881099 added a commit that referenced this pull request Mar 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants