Dapper 返回零引导

问题描述 投票:0回答:1

Dapper 不断返回默认值而不是 ID。可能是什么问题以及如何解决它? (短小精悍 + PostgreSQL)

你能解释一下问题是什么吗? 在此输入图片描述

public async Task<Appointment?> GetByIdWithPatientAndDoctorAsync(Guid id)
    {
        await using var connection = new NpgsqlConnection(connectionString);
        await connection.OpenAsync();
        const string query = """
                                 select a.*, 
                                        p.*, 
                                        d.*, 
                                        c.*, 
                                        s.name as specializationname
                                 from 
                                     appointments a
                                 inner join 
                                     patients p on a.patientid = p.id
                                 inner join 
                                     doctors d on a.doctorid = d.id
                                 inner join 
                                     clinics c on a.clinicid = c.id
                                 inner join 
                                     specializations s on d.specializationid = s.id
                                 where 
                                     a.id = @Id
                             """;

        var appointments = await connection.QueryAsync<Appointment, Patient, Doctor, Clinic, Specialization, Appointment>(
            query,
            (appointment, patient, doctor, clinic, specialization) =>
            {
                appointment.Patient = patient;
                doctor.Specialization = specialization;
                appointment.Doctor = doctor;
                appointment.Clinic = clinic;
                return appointment;
            },
            new { Id = id },
            splitOn: "id, id, id, specializationname");

        return appointments.FirstOrDefault();
    }
public class Appointment : Entity
{
    public Appointment() {}
    public Appointment(DateTime date, Guid patientId, Guid doctorId, Guid clinicId, AppointmentStatus appointmentStatus)
    {
        Date = date;
        PatientId = patientId;
        DoctorId = doctorId;
        ClinicId = clinicId;
        AppointmentStatus = appointmentStatus;
    }

    public DateTime Date { get; set; }
    public Guid PatientId { get; set; }
    public Patient Patient { get; set; }
    public Guid DoctorId { get; set; }
    public Doctor Doctor { get; set; }
    public Guid ClinicId { get; set; }
    public Clinic Clinic { get; set; }
    public AppointmentStatus AppointmentStatus { get; set; }
}
public abstract class Entity
{
    protected Entity() {}

    protected Entity(Guid id)
    {
        Id = id;
    }
    
    public Guid Id { get; }
}

我已经尝试过使用 spliton 并通过 ID 引导到另一个名称,但没有任何帮助

c# asp.net-core orm dapper guid
1个回答
0
投票

正如评论中提到的,在

Entity
类中
Id
属性不包含 setter。因此它没有被赋值,默认是
Guid.Empty

添加 setter 应该可以解决问题。

public abstract class Entity
{
    protected Entity() {}

    protected Entity(Guid id)
    {
        Id = id;
    }
    
    public Guid Id { get; set; }
}
© www.soinside.com 2019 - 2024. All rights reserved.