diff --git a/Extensions/FreeSql.Extensions.AggregateRoot/AggregateRootRepository/AggregateRootRepository.cs b/Extensions/FreeSql.Extensions.AggregateRoot/AggregateRootRepository/AggregateRootRepository.cs index 33ce0b744..e277f7169 100644 --- a/Extensions/FreeSql.Extensions.AggregateRoot/AggregateRootRepository/AggregateRootRepository.cs +++ b/Extensions/FreeSql.Extensions.AggregateRoot/AggregateRootRepository/AggregateRootRepository.cs @@ -74,7 +74,6 @@ public void AsTable(Func rule) _asTableRule = rule; } public Type EntityType => _repository.EntityType; - public IDataFilter DataFilter => _repository.DataFilter; public void Attach(TEntity entity) { diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml index 3a86a24b7..394dc3681 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.xml +++ b/FreeSql.DbContext/FreeSql.DbContext.xml @@ -480,6 +480,13 @@ FreeSql.Repository Update 失败,因为设置了过滤器 {filterKey}: {filterValueExpression},更新的数据不符合{entityString} + + + 在工作单元内创建联合主键的仓储类,工作单元下的仓储操作具有事务特点 + + + + 动态Type,在使用 Repository<object> 后使用本方法,指定实体类型 diff --git a/FreeSql.DbContext/Repository/ContextSet/RepositoryUnitOfWork.cs b/FreeSql.DbContext/Repository/ContextSet/RepositoryUnitOfWork.cs new file mode 100644 index 000000000..24e29736a --- /dev/null +++ b/FreeSql.DbContext/Repository/ContextSet/RepositoryUnitOfWork.cs @@ -0,0 +1,31 @@ +using System; +using System.Linq.Expressions; + +namespace FreeSql +{ + + public interface IRepositoryUnitOfWork : IUnitOfWork + { + /// + /// 在工作单元内创建联合主键的仓储类,工作单元下的仓储操作具有事务特点 + /// + /// + /// + IBaseRepository GetRepository() where TEntity : class; + } + + class RepositoryUnitOfWork : UnitOfWork, IRepositoryUnitOfWork + { + + public RepositoryUnitOfWork(IFreeSql fsql) : base(fsql) + { + } + + public IBaseRepository GetRepository() where TEntity : class + { + var repo = new DefaultRepository(_fsql); + repo.UnitOfWork = this; + return repo; + } + } +} diff --git a/FreeSql.Tests/FreeSql.Tests.DbContext2/AggregateRootRepositoryTest2.cs b/FreeSql.Tests/FreeSql.Tests.DbContext2/AggregateRootRepositoryTest2.cs index c2c1affdf..448fced1f 100644 --- a/FreeSql.Tests/FreeSql.Tests.DbContext2/AggregateRootRepositoryTest2.cs +++ b/FreeSql.Tests/FreeSql.Tests.DbContext2/AggregateRootRepositoryTest2.cs @@ -21,6 +21,22 @@ public OrderRepository(IFreeSql fsql, UnitOfWorkManager uowManager) : base(uowMa public override ISelect Select => base.SelectDiy; } + [Fact] + public void Test3() + { + using (var fsql = g.CreateMemory()) + { + var sql01 = fsql.Select() + .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() { diff --git a/FreeSql/Internal/CommonExpression.cs b/FreeSql/Internal/CommonExpression.cs index e2e979339..1cab1b986 100644 --- a/FreeSql/Internal/CommonExpression.cs +++ b/FreeSql/Internal/CommonExpression.cs @@ -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();