From 1f7e97869948d2083c721085cba2468190c8fff8 Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Thu, 7 Mar 2024 18:18:25 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BF=AE=E5=A4=8D=20=E5=BC=B1=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=20IBaseRepository=20=E7=BA=A7=E8=81=94?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E9=97=AE=E9=A2=98=EF=BC=9B#1740?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Examples/base_entity/Program.cs | 80 ++++++------------- FreeSql.DbContext/FreeSql.DbContext.xml | 9 +++ .../ContextSet/RepositoryDbContext.cs | 5 +- 3 files changed, 36 insertions(+), 58 deletions(-) diff --git a/Examples/base_entity/Program.cs b/Examples/base_entity/Program.cs index 9e29e5a4..f2cf67fc 100644 --- a/Examples/base_entity/Program.cs +++ b/Examples/base_entity/Program.cs @@ -14,6 +14,7 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Npgsql; using System; +using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.ComponentModel; @@ -610,72 +611,39 @@ namespace base_entity BaseEntity.Initialization(fsql, () => _asyncUow.Value); #endregion - Expression, bool>> where111 = null; - //where111 = where111.Or(a => a.t6.Sort > 10); - var tsqlqlq1 = fsql.Select().Where(where111).ToSql(); + var fsql2 = fsql; + // 动态构建实体类型,树形结构,引用自身类型 + var areaBuilder = fsql2.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 }); - // 交叉引用类型,先定义两个类型,再Build - var channelBuilder = fsql.CodeFirst.DynamicEntity("Channel", new TableAttribute { Name = "dm_channel" }); - var channelGroupBuilder = fsql.CodeFirst.DynamicEntity("ChannelGroup", new TableAttribute { Name = "dm_channel_group" }); + // builder.TypeBuilder可作为类型被引用 + areaBuilder.Property("parent", areaBuilder.TypeBuilder, new NavigateAttribute { Bind = "parentId" }) + .Property("children", typeof(List<>).MakeGenericType(areaBuilder.TypeBuilder), new NavigateAttribute { Bind = "parentId" }); - 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); + var table = areaBuilder.Build(); // 迁移 - fsql.CodeFirst.SyncStructure(channelEntityType, channelGroupEntityType); + fsql2.CodeFirst.SyncStructure(table.Type); - fsql.Delete().AsType(channelEntityType).Where("1=1").ExecuteAffrows(); - fsql.Delete().AsType(channelGroupEntityType).Where("1=1").ExecuteAffrows(); + var area1 = table.CreateInstance(new Dictionary { ["id"] = 1, ["name"] = "北京" }); + var area2 = table.CreateInstance(new Dictionary { ["id"] = 2, ["parentId"] = 1, ["name"] = "东城区" }); + var area3 = table.CreateInstance(new Dictionary { ["id"] = 3, ["parentId"] = 1, ["name"] = "西城区" }); - // 创建实体对象 - var channelGroup = channelGroupTable.CreateInstance(new Dictionary - { - ["id"] = 1, - ["name"] = "央视频道", - }); - var c = fsql.Insert().AsType(channelGroupEntityType) - .AppendData(channelGroup) - .ExecuteAffrows(); - Console.WriteLine($"{c} inserted"); + var area1Children = Activator.CreateInstance(typeof(List<>).MakeGenericType(table.Type)) as IList; + area1Children!.Add(area2); + area1Children!.Add(area3); + table.Type.GetProperty("children")!.SetValue(area1, area1Children); - var channel = channelTable.CreateInstance(new Dictionary - { - ["id"] = 1, - ["name"] = "CCTV-1", - ["channelGroupId"] = 1 - }); - c = fsql.Insert().AsType(channelEntityType) - .AppendData(channel) - .ExecuteAffrows(); - Console.WriteLine($"{c} inserted"); + fsql2.Delete().AsType(table.Type).Where("1=1").ExecuteAffrows(); - // 运行正确 - var list111 = fsql.Select().AsType(channelGroupEntityType) - .IncludeByPropertyName("channels") - .ToList(); + var testRepo = fsql2.GetRepository(); + testRepo.AsType(table.Type); + testRepo.Insert(area1); + testRepo.SaveMany(area1, "children"); - var repo222 = fsql.GetRepository(); - repo222.AsType(channelGroupEntityType); - - // 运行错误 - var list222 = repo222.Select - .IncludeByPropertyName("channels") - .ToList(); diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml index 6c9bef48..197e6480 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.xml +++ b/FreeSql.DbContext/FreeSql.DbContext.xml @@ -826,5 +826,14 @@ + + + 批量注入 Repository,可以参考代码自行调整 + + + + + + diff --git a/FreeSql.DbContext/Repository/ContextSet/RepositoryDbContext.cs b/FreeSql.DbContext/Repository/ContextSet/RepositoryDbContext.cs index 14d4556a..be0616a6 100644 --- a/FreeSql.DbContext/Repository/ContextSet/RepositoryDbContext.cs +++ b/FreeSql.DbContext/Repository/ContextSet/RepositoryDbContext.cs @@ -34,8 +34,9 @@ namespace FreeSql repo = Activator.CreateInstance(typeof(DefaultRepository<,>).MakeGenericType(entityType, typeof(int)), _repo.Orm); (repo as IBaseRepository).UnitOfWork = _repo.UnitOfWork; GetRepositoryDbField(entityType, "_dbPriv").SetValue(repo, this); - GetRepositoryDbField(entityType, "_asTablePriv").SetValue(repo, - GetRepositoryDbField(_repo.EntityType, "_asTablePriv").GetValue(_repo)); + GetRepositoryDbField(entityType, "_asTablePriv").SetValue(repo, + _repo.GetType().GetField("_asTablePriv", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(_repo)); + //GetRepositoryDbField(_repo.EntityType, "_asTablePriv").GetValue(_repo)); if (typeof(IBaseRepository<>).MakeGenericType(_repo.EntityType).IsAssignableFrom(_repo.GetType())) typeof(RepositoryDbContext).GetMethod("SetRepositoryDataFilter").MakeGenericMethod(_repo.EntityType)