仓储模式下怎么多表查询 返回

SqlSugar 老数据
4 2451
该叫什么 love-y 发布于2020/12/4
悬赏:5 飞吻

本人初次接触国产ORM 首先感谢有这么好的一个平台。在学习过程中遇到个问题,就是如何在仓储模式中多表查询。在示例中虽然提到了调用外部仓储 

image.png

就拿demo中的实体来说 现在想咨询的问题是 如何让 OrderItem Order这两个表关联查询?麻烦老司机带路 谢谢!

热忱回答4

  • 仓储默认方法是不支持多表的,你可以在Repository这个类里面添加 多表的支持 

    0 回复
  • public class Repository<T> : SimpleClient<T> where T : class, new()
    {
        public Repository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null
        {
            if (context == null)
            {
                base.Context = new SqlSugarClient(new ConnectionConfig()
                {
                    DbType = SqlSugar.DbType.SqlServer,
                    InitKeyType = InitKeyType.Attribute,
                    IsAutoCloseConnection = true,
                    ConnectionString = Config.ConnectionString
                });
            }
        }
     
        /// <summary>
        /// 扩展方法,自带方法不能满足的时候可以添加新方法
        /// </summary>
        /// <returns></returns>
        public List<ResultType> 多表查询<T1,T2,ResultType>(JoinType type, JOIN表达式 , Where表达式等  )
        {
           return 表ase.context.queryable<t1,t2>(join表达式).where(where表达式).Select<ResultType>().ToList();
        }
         
    }

    大概这个样子,我是手写的你理解意思去写,不要照抄

    0 回复
  • love-y love-y VIP0
    2020/12/5

    @fate stay night:这个join表达式如何写呢?麻烦您看下。

            public List<ResultType> 多表查询<T1, T2, ResultType>(JoinType type, JOIN表达式, List<IConditionalModel> conditionals)
            {
                return base.Context.Queryable<T1, T2>(JOIN表达式).Where(conditionals)
                            .Select<ResultType>().ToList();
            }


    0 回复
  • @love-y:F12看我JOIN的时候怎么定义的你也怎么定义

    0 回复