使用代码优先将多个导航属性设置为相同的 table

multiple navigation properties to the same table using code first

我有一个一对多的关系设置。 IT 工作正常,但现在我正在尝试使用 pdfsharp,我需要一些有关导航属性的帮助。每个测量有 4 张图片。前、后、右、左。我需要在 pdf 中包含所有 4 张图片。我无法用我现在拥有的东西联系到他们。我可以 return 每次测量的图片列表 table。只是不知道如何更改它以使其也能正常工作。

 public class Measurement
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
    public Guid Id { get; set; }
    public DateTime? MeasurementDate { get; set; }
    public decimal? BustMeasurement { get; set; }
    public decimal? ChestMeasurement { get; set; }
    public decimal? FAT { get; set; }

    public string MeasurementUploadBy { get; set; }
    public DateTime? MeasurementUploadDate { get; set; }
    public DateTime? MeasurementEditDate { get; set; }
    public string MeasurementEditBy { get; set; }

    public int? ClientId { get; set; }
    [ForeignKey("ClientId")]
    public virtual Client Client { get; set; }

    public virtual ICollection<Picture> Pictures { get; set; }
}

  public class Picture
{
    public int Id { get; set; }
    public string PictureFrontUrl { get; set; }
    public string PictureBackUrl { get; set; }
    public string PictureRightSideUrl { get; set; }
    public string PictureLeftSideUrl { get; set; }
    public string PictureNote { get; set; }
    public string PictureUploadBy { get; set; }
    public DateTime? PictureUploadDate { get; set; }
    public DateTime? PictureDate { get; set; }
    public string ClientName { get; set; }
    public string PictureType { get; set; }
    public Guid MeasurementId { get; set; }
    [ForeignKey("MeasurementId")]
    public virtual Measurement Measurement { get; set; }
}

MeasurementApi

public Measurement GetMeasurement(Guid id)
    {
        using (var context = new ApplicationDbContext())
        {
            Measurement model = new Measurement();
            model = context.Measurements
               .Include(x => x.Pictures)
               .FirstOrDefault(j => j.Id == id);
            return model;
        }
    }

PDFSharp GEt 调用

apiMeasurementController adapter = new apiMeasurementController();
        Measurement model = new Measurement();
        model = adapter.GetMeasurement(id);

如果你想让你的测量有 4 张图片,而不是图片的集合,你应该有这样的东西:

public class Measurement
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
    public Guid Id { get; set; }

    ....

    // Won't need these
    // public virtual ICollection<Picture> Pictures { get; set; }


    public virtual Picture LeftPicture { get; set; }

    public virtual Picture TopPicture { get; set; }

    public virtual Picture RightPicture { get; set; }

    public virtual Picture BottomPicture { get; set; }
}

还有你的照片:

public class Picture
{
    public int Id { get; set; }

    // Don't need these 4 any more.
    // public string PictureFrontUrl { get; set; }
    // public string PictureBackUrl { get; set; }
    // public string PictureRightSideUrl { get; set; }
    // public string PictureLeftSideUrl { get; set; }

    public string PictureUrl { get; set; }

    public string PictureNote { get; set; }
    public string PictureUploadBy { get; set; }
    public DateTime? PictureUploadDate { get; set; }
    public DateTime? PictureDate { get; set; }
    public string ClientName { get; set; }
    public string PictureType { get; set; }
    public Guid MeasurementId { get; set; }
    [ForeignKey("MeasurementId")]
    public virtual Measurement Measurement { get; set; }
}

在你的API

public Measurement GetMeasurement(Guid id)
{
    using (var context = new ApplicationDbContext())
    {
        Measurement model = new Measurement();
        model = context.Measurements
           .Include(x => x.LeftPicture)
           .Include(x => x.TopPicture)
           .Include(x => x.RightPicture)
           .Include(x => x.BottomPicture)
           .FirstOrDefault(j => j.Id == id);
        return model;
    }
}