Skip to content

Commit

Permalink
- 修复 多表 ToList 非 a,b,c 别名相同实体 bug;#1830 #1861
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Jul 24, 2024
1 parent b024c54 commit 04a538a
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public void AsTable(Func<Type, string, string> rule)
_asTableRule = rule;
}
public Type EntityType => _repository.EntityType;
public IDataFilter<TEntity> DataFilter => _repository.DataFilter;

public void Attach(TEntity entity)
{
Expand Down
7 changes: 7 additions & 0 deletions FreeSql.DbContext/FreeSql.DbContext.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions FreeSql.DbContext/Repository/ContextSet/RepositoryUnitOfWork.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Linq.Expressions;

namespace FreeSql
{

public interface IRepositoryUnitOfWork : IUnitOfWork
{
/// <summary>
/// 在工作单元内创建联合主键的仓储类,工作单元下的仓储操作具有事务特点
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <returns></returns>
IBaseRepository<TEntity> GetRepository<TEntity>() where TEntity : class;
}

class RepositoryUnitOfWork : UnitOfWork, IRepositoryUnitOfWork
{

public RepositoryUnitOfWork(IFreeSql fsql) : base(fsql)
{
}

public IBaseRepository<TEntity> GetRepository<TEntity>() where TEntity : class
{
var repo = new DefaultRepository<TEntity, int>(_fsql);
repo.UnitOfWork = this;
return repo;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ public OrderRepository(IFreeSql fsql, UnitOfWorkManager uowManager) : base(uowMa
public override ISelect<Order> Select => base.SelectDiy;
}

[Fact]
public void Test3()
{
using (var fsql = g.CreateMemory())
{
var sql01 = fsql.Select<Order, OrderDetail, OrderDetail>()
.InnerJoin((x, y, z) => y.OrderId == x.Id)
.InnerJoin((x, y, z) => z.OrderId == x.Id)
.ToSql((x, y, z) => new { x, y, z });
Assert.Equal(@"SELECT a.""Id"" as1, a.""Field2"" as2, b.""Id"" as3, b.""OrderId"" as4, b.""Field4"" as5, c.""Id"" as6, c.""OrderId"" as7, c.""Field4"" as8
FROM ""Order"" a
INNER JOIN ""OrderDetail"" b ON b.""OrderId"" = a.""Id""
INNER JOIN ""OrderDetail"" c ON c.""OrderId"" = a.""Id""", sql01);
}
}

[Fact]
public void Test2()
{
Expand Down
10 changes: 9 additions & 1 deletion FreeSql/Internal/CommonExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1899,7 +1899,15 @@ public string ExpressionLambdaToSql(Expression exp, ExpTSC tsc)
finds = tsc._tables.Where(a => a.Table.Type == tbtmp.Type && a.Alias == alias).ToArray();
if (finds.Any() == false && alias.Contains("__") == false)
finds = tsc._tables.Where(a => a.Table.Type == tbtmp.Type).ToArray();
if (finds.Any()) finds = new[] { finds.First() };
if (finds.Any())
{
if (finds.Length > 1 && isa && parmExp != null) //非 a,b,c 多表查询时 #1830
{
var tmpFinds = tsc._tables.Where(a => a.Parameter == parmExp).ToArray();
if (tmpFinds.Any()) finds = tmpFinds;
}
if (finds.Any()) finds = new[] { finds.First() };
}
}
if (finds.Length != 1 && isa && parmExp != null)
finds = tsc._tables.Where(a => a.Parameter == parmExp).ToArray();
Expand Down

0 comments on commit 04a538a

Please sign in to comment.