代码之家  ›  专栏  ›  技术社区  ›  user384884

Dapper返回零guid

  •  0
  • user384884  · 技术社区  · 2 年前

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

    你能解释一下问题出在哪里吗? enter image description here

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

    我已经试过和splion一起玩,并以ID的身份转到另一个名字,但没有任何帮助

    1 回复  |  直到 2 年前
        1
  •  0
  •   Yong Shun    2 年前

    如评论中所述,在 Entity Id 属性不包含setter。因此,它没有赋值,默认情况下是 Guid.Empty .

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

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