繁体   English   中英

首先使用代码将多个导航属性复制到同一张表

[英]multiple navigation properties to the same table using code first

我有一对多关系设置。 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电话

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

如果您想让Measurement拥有4张图片,而不是一张Colleciton图片,则应该具有以下内容:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM