diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml index 68241282..312b0c42 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.xml +++ b/FreeSql.DbContext/FreeSql.DbContext.xml @@ -130,6 +130,13 @@ 清空状态数据 + + + 根据 lambda 条件删除数据 + + + + 添加 @@ -525,5 +532,14 @@ + + + 批量注入 Repository,可以参考代码自行调整 + + + + + + diff --git a/FreeSql.Tests/FreeSql.Tests/MySql/Curd/MySqlSelectTest.cs b/FreeSql.Tests/FreeSql.Tests/MySql/Curd/MySqlSelectTest.cs index 0ee8ba66..3dc86b69 100644 --- a/FreeSql.Tests/FreeSql.Tests/MySql/Curd/MySqlSelectTest.cs +++ b/FreeSql.Tests/FreeSql.Tests/MySql/Curd/MySqlSelectTest.cs @@ -512,6 +512,20 @@ namespace FreeSql.Tests.MySql sql = query.ToSql().Replace("\r\n", ""); Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a LEFT JOIN TestTypeInfo b on b.Guid = a.TypeGuid and b.Name = ?bname", sql); query.ToList(); + + sql = select.ToSql(a => a.Type.Parent); + Assert.Equal(@"SELECT a__Type__Parent.`Id` as1, a__Type__Parent.`Name` as2 +FROM `tb_topic` a +LEFT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid` +LEFT JOIN `TestTypeParentInfo` a__Type__Parent ON a__Type__Parent.`Id` = a__Type.`ParentId`", sql); + var ccdata1 = select.ToList(a => a.Type.Parent); + + sql = select.Include(a => a.Type.Parent).ToSql(a => a.Type); + Assert.Equal(@"SELECT a__Type.`Guid` as1, a__Type.`ParentId` as2, a__Type.`Name` as3, a__Type__Parent.`Id` as4, a__Type__Parent.`Name` as5 +FROM `tb_topic` a +LEFT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid` +LEFT JOIN `TestTypeParentInfo` a__Type__Parent ON a__Type__Parent.`Id` = a__Type.`ParentId`", sql); + var ccdata2 = select.Include(a => a.Type.Parent).ToList(a => a.Type); } [Fact] public void InnerJoin() diff --git a/FreeSql/Internal/CommonExpression.cs b/FreeSql/Internal/CommonExpression.cs index f2d1512a..d6382158 100644 --- a/FreeSql/Internal/CommonExpression.cs +++ b/FreeSql/Internal/CommonExpression.cs @@ -83,7 +83,7 @@ namespace FreeSql.Internal return false; case ExpressionType.Parameter: case ExpressionType.MemberAccess: - if (_common.GetTableByEntity(exp.Type) != null && + if (_common.GetTableByEntity(exp.Type) != null && //判断 [JsonMap] 并非导航对象 (exp.NodeType == ExpressionType.Parameter || exp is MemberExpression expMem && ( _common.GetTableByEntity(expMem.Expression.Type)?.ColumnsByCs.ContainsKey(expMem.Member.Name) == false || @@ -112,6 +112,30 @@ namespace FreeSql.Internal if (index >= 0) field.Append(_common.FieldAsAlias($"as{++index}")); parent.Childs.Add(child); } + if (_tables.Count > 1) { //如果下级导航属性被 Include 过,则将他们也查询出来 + foreach (var memProp in tb.Properties.Values) + { + var memtbref = tb.GetTableRef(memProp.Name, false); + if (memtbref == null) continue; + switch (memtbref.RefType) + { + case TableRefType.ManyToMany: + case TableRefType.OneToMany: + continue; + } + if (_tables.Any(a => a.Alias == $"{map.First().Table.Alias}__{memProp.Name}") == false) continue; + + var child = new ReadAnonymousTypeInfo + { + Property = memProp, + CsName = memProp.Name, + CsType = memProp.PropertyType, + MapType = memProp.PropertyType + }; + parent.Childs.Add(child); + ReadAnonymousField(_tables, field, child, ref index, Expression.MakeMemberAccess(exp, memProp), grouping, whereCascadeExpression, false); + } + } } else {