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 引导到另一个名称,但没有任何帮助
正如评论中提到的,在
Entity
类中 Id
属性不包含 setter。因此它没有被赋值,默认是Guid.Empty
。
添加 setter 应该可以解决问题。
public abstract class Entity
{
protected Entity() {}
protected Entity(Guid id)
{
Id = id;
}
public Guid Id { get; set; }
}