Questions about ASP.NET MVC project migration to ASP.NET CORE entity class can not access its related entity classes by foreign keys

 

problem:

Entity classes:

Comment entity class has a foreign key 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 entity class, corresponding to a plurality 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; }
        
    }

But we see the line 12 in the figure, Commets talk by navigating to its associated data acquisition

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()
                    })
                })
            });

However, by Postman testing, we found commentData was empty, but my foreign key is a clearly written

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

Can not find what the problem, only in a different wording

The

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

Changed

 

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

After the code changes 

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 use test data damn finally came out:

{
    "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"
                }
            ]
        }
    ]
}

 

Published 105 original articles · won praise 17 · views 110 000 +

Guess you like

Origin blog.csdn.net/qq_38890412/article/details/103850233