简体   繁体   English

将外键值包含到单个记录的 DTO 中

[英]Including foreign key values into a DTO for a single record

It's been a while since I have done this, but I know there is an easy way to do this that I have forgotten.我已经有一段时间没有这样做了,但我知道有一种简单的方法可以做到这一点,但我已经忘记了。 Below I have a class designed to populate a single record of a data object.下面我有一个 class 设计用于填充数据 object 的单个记录。 But I cannot get the values from another table (related by foreign key) to populate using the lambda statement because I am missing something (the two values being pulled in from another table below can be seen as dto.LeaseName and dto.CarName).但是我无法使用 lambda 语句从另一个表(与外键相关)中获取值来填充,因为我遗漏了一些东西(从下面的另一个表中提取的两个值可以看作 dto.LeaseName 和 dto.CarName)。 How should I write the lambda for the object dm?我应该如何为 object dm 编写 lambda?

    public StationUnloadingLogDTO GetSingleRecordforLog(int Id)
    {
        StationUnloadingLogDTO dto = new StationUnloadingLogDTO();
        StationUnloadingLog dm = new StationUnloadingLog();

        dm = entity.StationUnloadingLog
                .Where(x => x.Id == Id)
                .FirstOrDefault();

        dto.Id = dm.Id;
        dto.DateLogged = dm.DateLogged;
        dto.DriverName = dm.DriverName;
        dto.TruckNumber = dm.TruckNumber;
        dto.CarName = dm.Carrier.CarName;
        dto.CarrierId = dm.CarrierId;
        dto.SpecificGravity = dm.SpecificGravity;
        dto.LactMeterOpen = dm.LactMeterOpen;
        dto.LactMeterClose = dm.LactMeterClose;
        dto.EstimatedBarrels = dm.EstimatedBarrels;
        dto.TicketNumber = dm.TicketNumber;
        dto.LeaseNumber = dm.LeaseNumber;
        dto.LeaseName = dm.Company.CmpName;
        dto.StationId = dm.StationId;

        return dto;
    }

Here are the related data classes下面是相关的数据类

namespace Data.Models
{
    public partial class Company
    {
        public Company()
        {
            StationUnloadingLog = new HashSet<StationUnloadingLog>();
        }

        public string CmpId { get; set; }
        public string CmpName { get; set; }
        public string CmpAddress1 { get; set; }
        public string CmpAddress2 { get; set; }
        public int? CmpCity { get; set; }
        public string CmpZip { get; set; }
        public string CmpPrimaryphone { get; set; }

        public ICollection<StationUnloadingLog> StationUnloadingLog { get; set; }
    }


    public class StationUnloadingLogDTO
    {
        public int Id { get; set; }
        public DateTime? DateLogged { get; set; }
        public string DriverName { get; set; }
        public string TruckNumber { get; set; }
        public string CarrierId { get; set; }
        public string CarName { get; set; }
        public decimal? SpecificGravity { get; set; }
        public decimal? LactMeterOpen { get; set; }
        public decimal? LactMeterClose { get; set; }
        public int? EstimatedBarrels { get; set; }
        public string TicketNumber { get; set; }
        public string LeaseName { get; set; }
        public string LeaseNumber { get; set; }
        public string StationId { get; set; }
    }

    public partial class StationUnloadingLog
    {
        public int Id { get; set; }
        public DateTime? DateLogged { get; set; }
        public string DriverName { get; set; }
        public string TruckNumber { get; set; }
        public string CarrierId { get; set; }
        public decimal? SpecificGravity { get; set; }
        public decimal? LactMeterOpen { get; set; }
        public decimal? LactMeterClose { get; set; }
        public int? EstimatedBarrels { get; set; }
        public string TicketNumber { get; set; }
        public string LeaseNumber { get; set; }
        public string StationId { get; set; }

        public Carrier Carrier { get; set; }
        public Company Company { get; set; }
        public Tractorprofile Tractorprofile { get; set; }
    }

    public partial class Carrier
    {
        public Carrier()
        {
            StationUnloadingLog = new HashSet<StationUnloadingLog>();
        }

        public string CarId { get; set; }
        public string CarName { get; set; }

        public string CarAddress1 { get; set; }
        public string CarAddress2 { get; set; }
        public int? CtyCode { get; set; }
        public string CarZip { get; set; }
        public string CarContact { get; set; }

        public ICollection<StationUnloadingLog> StationUnloadingLog { get; set; }
}

You should query for your record with child entities like this.您应该使用这样的子实体查询您的记录。

dm = DbSet<StationUnloadingLog>
                .Where(x => x.Id == Id).Include(x => x.Carrrier)
                .FirstOrDefault();

You are right, include does the trick.你是对的,包含就可以了。 I had to remember to add using Microsoft.EntityFrameworkCore to the page.我必须记住使用 Microsoft.EntityFrameworkCore 添加到页面。 Forgetting that I think is what confused me, but now I can populate those fields from other tables into the dto.忘记我认为是什么让我感到困惑,但现在我可以将其他表中的这些字段填充到 dto 中。 Thanks!谢谢!

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

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