簡體   English   中英

在C#ASP.NET Web API中使用2個外鍵從表中檢索數據

[英]retrieving data from a table with 2 foreign keys in c# asp.net web api

我試圖從具有2個外鍵的表中檢索數據,並使用where子句對其進行過濾。 它是一個Web API場景,當我調用該方法的url端點時,它將返回http代碼200,但沒有數據,也不會返回任何錯誤。 下面是模型,這是我的表格的基礎

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace xxxx.Models
{
    public class Feedback
    {
        [Key]
        public int Id { get; set; }
        [Required]
        public string comment { get; set; }
        public string date { get; set; }

        //Foreign key
        [Required]
        public int projectId { get; set; }
        public int studentId { get; set; }
        public int companyId { get; set; }

        [ForeignKey("studentId")]
        public  Student student { get; set; }

        [ForeignKey("companyId")]
        public Company company { get; set; }

    }
}

控制器中的代碼如下,該代碼具有我用來檢索數據的方法

    [Route("GetComments")]
    [HttpGet]
    public IQueryable<CommentDTO> GetFeedbacks(int project_id)
    {
        var comments = from b in db.Feedbacks.Where(b => b.projectId == project_id)
                       .Include(b=> b.company)
                       .Include(b=> b.student)

                       select new CommentDTO
                       {
                           Id = b.Id,
                           comment = b.comment,
                           date = b.date,
                           student = new StudentCommentDTO()
                           {
                               student_number = b.student.Id,
                               first_name = b.student.firstName,
                               middle_name = b.student.middleName,
                               last_name = b.student.lastName,
                               profile_pic = b.student.profilePic
                           },
                           company = new CompanyCommentDTO()
                           {
                               companyID = b.company.Id,
                               profile_pic = b.company.profilePicture,
                               name = b.company.companyName
                           }
                       };
        return comments;
    }

請注意,StudentCommentDTO只是一個數據傳輸對象,我也嘗試刪除了where子句,但是它仍然沒有用,我還嘗試了使我的導航屬性變為虛擬,但結果仍然相同。 我可以大膽地確認表中是否有數據。 下面的屏幕截圖顯示了我在提琴手中得到的結果,總是返回一個空數組。 單擊此處查看提琴手的結果

我想我已經知道問題出在哪里了,該反饋表存儲了我想要的數據,該數據必須存儲在StudentCompany的外鍵中,在每種情況下,它們中的一個必須為null(我可以使其成為可為空)。 我正在嘗試歸檔一種情況,在這種情況下,具有兩種不同角色的人,公司和學生可以參與對特定帖子的評論

我假設您的studentIdStudent班的Id 請記住,如果任何Id為null則返回列表中將缺少完整的join記錄(原因:不能在nullnull )。

        IQueryable<CommentDTO> comments = null;
        comments = from b in db.Feedbacks.Where(b => b.projectId == project_id)
                       join std in db.Students on b.studentId equals std.Id
                       join cmp in db.Companies on b.companyId equals cmp.Id 

                       select new CommentDTO
                       {
                           Id = b.Id,
                           comment = b.comment,
                           date = b.date,
                           student = new StudentCommentDTO()
                           {
                               student_number = std.Id,
                               first_name = std.firstName,
                               middle_name = std.middleName,
                               last_name = std.lastName,
                               profile_pic = std.profilePic
                           },
                           company = new CompanyCommentDTO()
                           {
                               companyID = cmp.Id,
                               profile_pic = cmp.profilePicture,
                               name = cmp.companyName
                           }
                       };
        return comments.ToList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM