From d2a7c52297d549de1968151177a0bc1bb7a64c70 Mon Sep 17 00:00:00 2001
From: 2881099 <2881099@qq.com>
Date: Sun, 1 May 2022 15:34:33 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20IncludeByProperty=20?=
=?UTF-8?q?=E6=89=A9=E5=B1=95=E6=96=B9=E6=B3=95=EF=BC=8C=E6=94=AF=E6=8C=81?=
=?UTF-8?q?=E9=9B=86=E5=90=88=E5=92=8C=E6=99=AE=E9=80=9A=E5=B1=9E=E6=80=A7?=
=?UTF-8?q?=EF=BC=9B?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FreeSql.DbContext/FreeSql.DbContext.xml | 9 ++
.../Sqlite/Curd/SqliteSelectTest.cs | 4 +-
FreeSql/Extensions/FreeSqlGlobalExtensions.cs | 115 ++++++++++++------
FreeSql/FreeSql.xml | 17 +--
4 files changed, 98 insertions(+), 47 deletions(-)
diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml
index da7ace6b..bdd16ff9 100644
--- a/FreeSql.DbContext/FreeSql.DbContext.xml
+++ b/FreeSql.DbContext/FreeSql.DbContext.xml
@@ -538,5 +538,14 @@
+
+
+ 批量注入 Repository,可以参考代码自行调整
+
+
+
+
+
+
diff --git a/FreeSql.Tests/FreeSql.Tests/Sqlite/Curd/SqliteSelectTest.cs b/FreeSql.Tests/FreeSql.Tests/Sqlite/Curd/SqliteSelectTest.cs
index be255299..2fa2cc42 100644
--- a/FreeSql.Tests/FreeSql.Tests/Sqlite/Curd/SqliteSelectTest.cs
+++ b/FreeSql.Tests/FreeSql.Tests/Sqlite/Curd/SqliteSelectTest.cs
@@ -1284,7 +1284,7 @@ WHERE (((cast(a.""Id"" as character)) in (SELECT b.""Title""
var by0 = g.sqlite.Select()
.Where(a => a.model2id <= model1.id)
.ToList();
- by0.IncludeMany(g.sqlite, "childs", "model2111Idaaa=model2id", 2, "id");
+ by0.IncludeByPropertyName(g.sqlite, "childs", "model2111Idaaa=model2id", 2, "id");
var t0 = g.sqlite.Select()
.IncludeMany(a => a.childs.Where(m3 => m3.model2111Idaaa == a.model2id))
@@ -1304,7 +1304,7 @@ WHERE (((cast(a.""Id"" as character)) in (SELECT b.""Title""
var by1 = g.sqlite.Select()
.Where(a => a.id <= model1.id)
.ToList();
- by1.IncludeMany(g.sqlite, "model2.childs", "model2111Idaaa=model2id");
+ by1.IncludeByPropertyName(g.sqlite, "model2.childs", "model2111Idaaa=model2id");
var t1 = g.sqlite.Select()
.IncludeMany(a => a.model2.childs.Where(m3 => m3.model2111Idaaa == a.model2.model2id))
.Where(a => a.id <= model1.id)
diff --git a/FreeSql/Extensions/FreeSqlGlobalExtensions.cs b/FreeSql/Extensions/FreeSqlGlobalExtensions.cs
index f7c88550..fed344ff 100644
--- a/FreeSql/Extensions/FreeSqlGlobalExtensions.cs
+++ b/FreeSql/Extensions/FreeSqlGlobalExtensions.cs
@@ -326,36 +326,77 @@ public static partial class FreeSqlGlobalExtensions
}
#endif
///
- /// 本方法实现从已知的内存 List 数据,进行和 ISelect.IncludeMany 相同功能的贪婪加载
- /// 示例:new List<Song>(new[] { song1, song2, song3 }).IncludeMany(fsql, "Tags", "ParentId=Id", 5, "Id,Name");
+ /// 本方法实现从已知的内存 List 数据,进行和 ISelect.IncludeMany/Include 相同功能的贪婪加载
+ /// 集合:new List<Song>(new[] { song1, song2, song3 }).IncludeByPropertyName(fsql, "Tags", "ParentId=Id", 5, "Id,Name");
+ /// 普通:new List<Song>(new[] { song1, song2, song3 }).IncludeByPropertyName(fsql, "Catetory");
+ /// ---普通属性 where/take/select 参数将无效
/// 文档:https://github.com/dotnetcore/FreeSql/wiki/%E8%B4%AA%E5%A9%AA%E5%8A%A0%E8%BD%BD
///
///
///
///
- /// 选择一个集合属性
- /// 设置临时的关系映射,格式:子类属性=T1属性
- /// 每个子集合只取条数
- /// 设置只查询部分字段
+ /// 选择一个集合或普通属性
+ /// 设置临时的子集合关系映射,格式:子类属性=T1属性
+ /// 设置子集合只取条数
+ /// 设置子集合只查询部分字段
///
///
- ///
- public static List IncludeMany(this List list, IFreeSql orm, string property, string where = null, int take = 0, string select = null) where T1 : class
+ public static List IncludeByPropertyName(this List list, IFreeSql orm, string property, string where = null, int take = 0, string select = null) where T1 : class
{
- if (list == null || list.Any() == false) return list;
- IncludeManyByPropertyNameCommonGetSelect(orm, property, where, take, select).SetList(list);
+ if (orm.CodeFirst.IsAutoSyncStructure)
+ {
+ var tb = orm.CodeFirst.GetTableByEntity(typeof(T1));
+ if (tb == null || tb.Primarys.Any() == false)
+ (orm.CodeFirst as CodeFirstProvider)._dicSycedTryAdd(typeof(T1)); //._dicSyced.TryAdd(typeof(TReturn), true);
+ }
+ var props = property.Split('.');
+ var t1tb = orm.CodeFirst.GetTableByEntity(typeof(T1));
+ var t1sel = orm.Select() as Select1Provider;
+ var t1expFul = t1sel.ConvertStringPropertyToExpression(property, true);
+ var t1exp = props.Length == 1 ? t1expFul : t1sel.ConvertStringPropertyToExpression(props[0], true);
+ if (t1expFul == null) throw new ArgumentException($"{nameof(property)} 无法解析为表达式树");
+ var propElementType = t1expFul.Type.GetGenericArguments().FirstOrDefault() ?? t1expFul.Type.GetElementType();
+ if (propElementType != null) //IncludeMany
+ {
+ if (props.Length > 1)
+ IncludeByPropertyName(list, orm, string.Join(".", props.Take(props.Length - 1)));
+ IncludeManyByPropertyNameCommonGetSelect(orm, property, where, take, select).SetList(list);
+ return list;
+ }
+ var tbtr = t1tb.GetTableRef(props[0], true);
+ if (tbtr == null) throw new ArgumentException($"{nameof(property)} 参数错误,它不是有效的导航属性");
+ var reftb = orm.CodeFirst.GetTableByEntity(t1exp.Type);
+ var refsel = orm.Select