[英]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.
IT工作正常,但现在我正在尝试使用pdfsharp,并且在导航属性方面需要一些帮助。 Every Measurement has 4 pictures.
每个测量都有4张图片。 Front, Back, Right, Left.
前,后,右,左。 I need to include all 4 pics in the pdf.
我需要在pdf中包括所有4张图片。 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 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 PDFSharp GEt电话
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: 如果您想让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; }
}
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 并在您的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.