簡體   English   中英

外部表外鍵值始終顯示 null ef core 3.1

[英]Foreign table foriegn key value always showing null ef core 3.1

為什么我的 Icollection 外鍵總是空白 我有一個名為 photos 的外部表,它是我使用 Icollection 創建的。 我使用 ef core 3.1.7 和 asp.net core 3.1 如何獲取文件附件 VesselsId 不是 null

基本上一艘船可以有很多照片,但它們也可以是很多艘船。

 public class Vessels  {
    public int Id { get; set; }
    [StringLength(400)]
    public string Name { get; set; }
    public string FlagName { get; set; }
    public ICollection<FileAttachments> PhotosAttachments { get; set; }

}

這是文件附件

public  class FileAttachments {
    public int Id { get; set; }
    public string Title { get; set; }
    public string File { set; get; }
}

在我希望將照片顯示為空白的地方,我使用 include 語句嘗試將它們包含在我的查詢中。

  var vessels = await _context.Vessels.Where(w=>w.Id==id).Include(c=>c.PhotosAttachments).FirstAsync();
  

但是,如果我看這里,當我查看字段值時,它會顯示 PhotosAttachments 為 null,它確實坐在那里 null。

在此處輸入圖像描述

我想我需要在這里做點什么,但我不確定是什么

protected override void OnModelCreating(ModelBuilder modelBuilder) {
  base.OnModelCreating(modelBuilder);
}

編輯 2

基本上我有一個通用的上傳文件方法,它通過提交按鈕調用

[HttpPost]
public async Task<IActionResult> UploadFiles(List<IFormFile> FormFile, int UploadArea, int PoiId, int VesselId) {     

       FileAttachments attachments = new FileAttachments {
       DocumentPath = filePath,
       UploadAreaId = UploadArea,
       CaseId = resultCaseId,                        
       FullPath = savedFileName,
       FileSize = infoFile.Length,
       OrignalFileName = fileAttachments.FileName,
       FileAttachmentType = fileAttachmentType,
       TennantId = await GetCurrentTennantId(),
       Extension = infoFile.Extension.Replace(".", "").ToLower(),
       UploadedBy = caseOfficer.Id,
       CreatedDate = DateTime.Now,
       File = uniqueFilename,
       ContentType = fileAttachments.ContentType,
       isActive = true,
       isDeleted = false
    };
    if (PoiId != 0)  {
       attachments.PoiID= PoiId;
    }
    if (VesselId != 0) {
        attachments.VesselId = VesselId;                       
     }
     _context.Add(attachments);
    await _context.SaveChangesAsync();
}

上面有一些混淆我用來存儲其他東西集合不創建這個字段它創建帶有額外 s 的 VesselsId 這是沒有填寫的內容。

 public int? VesselId { get; set; }

該集合創建此字段

在此處輸入圖像描述

像這樣添加與FileAttachments model 的關系

    public  class FileAttachments {
       ...
       [ForeignKey("Vessels")]
       public int? VesselId { get; set; }

       public Vessels Vessels { get; set; }
    }

暫無
暫無

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

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