Включение значений внешнего ключа в DTO для отдельной записи - PullRequest
0 голосов
/ 04 октября 2019

Прошло много времени с тех пор, как я это сделал, но я знаю, что есть простой способ сделать это, что я забыл. Ниже у меня есть класс, предназначенный для заполнения одной записи объекта данных. Но я не могу получить значения из другой таблицы (связанной с внешним ключом) для заполнения с помощью лямбда-оператора, потому что мне чего-то не хватает (два значения, извлекаемые из другой таблицы ниже, можно рассматривать как dto.LeaseName и dto.CarName). Как мне написать лямбду для объекта dm?

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

Вот соответствующие классы данных

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

Ответы [ 2 ]

0 голосов
/ 04 октября 2019

Вы правы, включайте трюк. Я должен был не забыть добавить использование Microsoft.EntityFrameworkCore на страницу. Меня смутило то, что я забыл, но теперь я могу заполнить эти поля из других таблиц в dto. Спасибо!

0 голосов
/ 04 октября 2019

Вы должны запросить свою запись с дочерними объектами, подобными этому.

dm = DbSet<StationUnloadingLog>
                .Where(x => x.Id == Id).Include(x => x.Carrrier)
                .FirstOrDefault();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...