欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

关于ASP.NET MVC项目迁移至ASP.NET CORE实体类通过外键访问不了其关联实体类的问题

程序员文章站 2022-05-23 23:29:07
...

 

问题:

实体类:

Comment实体类有一个外键TalkId

public class Comment
    {
        public virtual Talk Talk { get; set; }


        public long Id { get; set; }
        public long TalkId { get; set; }
        public string Point { get; set; }
        public DateTime CommentTime { get; set; }
        public long UserId { get; set; }
        public string Commenter { get; set; }   
        
    }

Talk实体类,一个Talk对应多个Comment

public class Talk
    {
        public Talk()
        {
            this.Comments = new List<Comment>();
        }
        public virtual List<Comment> Comments { get; set; }
        public virtual User User { get; set; }

        public long Id { get; set; }
        public long UserId { get; set; }
        public string Content { get; set; }
        public int Praise { get; set; }
        public DateTime TalkTime { get; set; }
        public int ForwordNumber { get; set; }
        
    }

但是我们请看下图的12行,通过talk导航至其关联的Commets获取数据

return Json(new
            {
                datas = talks.Select(s => new
                {
                    s.Id,
                    Username = db.Users.Where(u => u.Id == s.UserId).Select(u => u.Username).FirstOrDefault(),
                    s.UserId,
                    s.Content,
                    s.Praise,
                    TalkTime = s.TalkTime.ToLongDateString() + " " + s.TalkTime.ToShortTimeString(),
                    CommentNumber = s.Comments.Count(),
                    commentData = s.Comments.Where(t=>t.TalkId==s.Id).Select(t => new
                    {
                        t.Id,
                        CommentUserId = t.UserId,
                        t.Commenter,
                        t.Point,
                        CommentTime = t.CommentTime.ToLongDateString() + " " + t.CommentTime.ToShortTimeString()
                    })
                })
            });

但是,通过Postman测试,我们发现commentData里是空的,但我的外键明明写的是对的

{
    "datas": [
        {
            "Id": 1,
            "Username": "NTU计嵌162的帅哥",
            "UserId": 1,
            "Content": "我踏马快疯了",
            "Praise": 0,
            "TalkTime": "2020年1月5日 22:09",
            "CommentNumber": 0,
            "commentData": []
        }
    ]
}

实在找不到问题出在哪,只能换一种写法

s.Comments.Where(t=>t.TalkId==s.Id)

改成了

 

db.Comments.Where(t=>t.TalkId==s.Id)

修改之后的代码 

return Json(new
            {
                datas = talks.Select(s => new
                {
                    s.Id,
                    Username = db.Users.Where(u => u.Id == s.UserId).Select(u => u.Username).FirstOrDefault(),
                    s.UserId,
                    s.Content,
                    s.Praise,
                    TalkTime = s.TalkTime.ToLongDateString() + " " + s.TalkTime.ToShortTimeString(),
                    CommentNumber = s.Comments.Count(),
                    commentData = db.Comments.Where(t=>t.TalkId==s.Id).Select(t => new
                    {
                        t.Id,
                        CommentUserId = t.UserId,
                        t.Commenter,
                        t.Point,
                        CommentTime = t.CommentTime.ToLongDateString() + " " + t.CommentTime.ToShortTimeString()
                    })
                })
            });

使用Postman测试,该死的数据终于出来了:

{
    "datas": [
        {
            "Id": 1,
            "Username": "NTU计嵌162的帅哥",
            "UserId": 1,
            "Content": "我踏马快疯了",
            "Praise": 0,
            "TalkTime": "2020年1月5日 22:09",
            "CommentNumber": 0,
            "commentData": [
                {
                    "Id": 1,
                    "CommentUserId": 1,
                    "Commenter": "计嵌162管润玮",
                    "Point": "我也快疯了",
                    "CommentTime": "2020年1月5日 22:09"
                }
            ]
        }
    ]
}

 

相关标签: .NET