Skip to content

多表查询中的第二点的导航属性的例子是不是有问题, #1992

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? No Sign in to your account

Open
zhiquanchi opened this issue Mar 9, 2025 · 8 comments
Open

Comments

@zhiquanchi
Copy link

zhiquanchi commented Mar 9, 2025

多表查询,导航属性
这篇文章中说

正确配置 【导航关系】后,不需要手工调用 LeftJoin

但是我的查询类似

[HttpGet]
        public List<Xuqiu> GetAllXuqius(int page=1, int size=10)
        {
            

            return fsql.Select<Xuqiu>()
                // .LeftJoin((x, i) => x.XuqiuId == i.XuqiuId)
                // .IncludeMany(b => b.Images)
                .Page(page, size)
                .ToList();
        }

却没有导航的表的结果。

@2881099
Copy link
Collaborator

2881099 commented Mar 10, 2025

什么版本

@zhiquanchi
Copy link
Author

什么版本

 [net8.0]: 
   Top-level Package                                    Requested   Resolved
   > FreeSql                                            3.5.107     3.5.107
   > FreeSql.Provider.PostgreSQL                        3.5.107     3.5.107

@2881099
Copy link
Collaborator

2881099 commented Mar 10, 2025

aop事件打印sql监视

@zhiquanchi
Copy link
Author

using FreeSql.DataAnnotations;

namespace crm.models
{
    public class Xuqiu
    {
        /// <summary>
        /// 需求编号
        /// </summary>
        [Column(IsIdentity = true, IsPrimary = true)]
        public int XuqiuId { get; set; }
        /// <summary>
        /// 需求标题
        /// </summary>
        public required string title { get; set; }
        /// <summary>
        /// 需求内容
        /// </summary>
        public string content { get; set; }
        /// <summary>
        /// 需求中的图片,导航到图片表,one to many
        /// </summary>
        [Navigate(nameof(Image.XuqiuId))]
        public List<Image> Images { get; set; }
        
        /// <summary>
        /// 需求的公司id
        /// </summary>
        public int CompanyId { get; set; }
        /// </summary>
        /// 新增这个需求的用户id
        /// </summary>
        public int UserId { get; set; }
        
    }
    
    public class Image
    {
        [Column(IsIdentity = true, IsPrimary = true)]
        public int ImageId { get; set; }
        public string ImageUrl { get; set; }
        /// <summary>
        /// 外键,导航到需求表
        /// </summary>
        [Navigate(nameof(Xuqiu.XuqiuId))]
        public int  XuqiuId { get; set; }
    }
    

}

查询语句

[HttpGet]
        public List<Xuqiu> GetAllXuqius(int page=1, int size=10)
        {
            

            return fsql.Select<Xuqiu>()
                // .LeftJoin((x, i) => x.XuqiuId == i.XuqiuId)
                // .IncludeMany(b => b.Images)
                .Page(page, size)
                .ToList();
        }

aop 打印的SQL

Sql:SELECT a."XuqiuId", a."title", a."content", a."CompanyId", a."UserId" 
FROM "Xuqiu" a
limit 10

@2881099
Copy link
Collaborator

2881099 commented Mar 11, 2025

你给的例子并没有使用过导航属性

@zhiquanchi
Copy link
Author

需要怎么修改呢,在字段上加上 [Navigate(nameof(GroupId))] 不是导航属性吗,还是说我的查询语句写的有问题。

@2881099
Copy link
Collaborator

2881099 commented Mar 11, 2025

.Where(a => a.Category.Parent.Id > 0)

这个和你上面发的代码,没有相似的地方啊,你的问题是?

@zhiquanchi
Copy link
Author

查询语句写错了,已经更新。

No Sign up for free to join this conversation on GitHub. Already have an account? No Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants