首先使用代码将多个导航属性放到同一个表中

时间:2015-03-22 00:18:01

标签: c# entity-framework asp.net-web-api2

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

 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 Call

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

1 个答案:

答案 0 :(得分:1)

如果您希望您的测量有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;
    }
}