简体   繁体   中英

multiple navigation properties to the same table using code first

I have a one-to-many relationship setup. IT is working fine but now I am trying to use pdfsharp and I need some help with the navigation properties. Every Measurement has 4 pictures. Front, Back, Right, Left. I need to include all 4 pics in the pdf. I am unable to reach them with what i have now. I can return the list of pictures per measurement in a table. Just dont know how to change it for this to work as well.

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

If you want your Measurement to have 4 pictures, and not a colleciton of pictures you should have something like this:

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

And your picture:

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

And in your 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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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