将多对多转换为父子关系

Convert many-to-many to parent-child relation

我有两个 类 用户和约会,它们曾经以多对多关系链接。但现在我必须能够管理这种关系的状态(我需要能够跟踪用户是否已接受约会或拒绝或尚未回复)。

类 映射到相应的 table 用户和约会。 两个 table 都通过 UserAppointments table 连接,它具有复合主键,包括用户的 fk 和约会的 fk table。

当我尝试将新的 UserAppointment 对象添加到一个约会对象

用户映射

 public class UserMap : ClassMap<User>
    {
        public UserMap()
        {
            Id(d => d.Id);
            Map(d => d.FirstName).Column("FirstName");
            Map(d => d.LastName).Column("LastName");
            Map(d => d.Email).Column("Email");            
            HasMany(u => u.UserAppointments);  
            Table("Users");

        }
    }

预约映射

public class AppointmentMap : ClassMap<Appointment>
    {
        public AppointmentMap()
        {
            Id(c => c.Id);
            HasMany(a => a.AppointmentParticipants).Inverse().Cascade.All();
            References(a => a.Location).Column("FkAddressId");
            Map(a => a.DateAndTime).Column("AppointmentDate").CustomType<UtcDateTimeType>();
            Map(a => a.Description).Column("AppointmentDescription");
            Map(a => a.Status).Column("AppointmentStatus").CustomType<AppointmentStatus>();
            HasMany(a => a.AppointmentEstates).Inverse().Cascade.All();
            Table("Appointments");
        }

UserAppointments 映射

public class UserAppointmentsMap : ClassMap<UserAppointment>
    {
        public UserAppointmentsMap()
        {
            CompositeId()
                .KeyReference(a => a.Appointment, "FkAppointmentsId")
                .KeyReference(u => u.User, "FkUserId");
            References(a => a.User).Column("FkUserId");
            References(a => a.Appointment).Column("FkAppointmentsId");
            Table("UserAppointments");
        }
    }

预期的结果是当我将新的 UserAppointment 添加到现有 Appointment 的集合时,将在 UserAppointments table 中创建一个新记录,指向相关的 User 和 Appointment 实体

我终于设法解决了这个问题....实际上我的代码有几个问题:

  1. "Invalid index 2 for this SqlParameterCollection with Count=2." 的问题是由 UserAppointmentsMap 引起的。一旦将 属性 定义为复合键的一部分,就不应将其再次映射为引用。

    public class UserAppointmentsMap : ClassMap<UserAppointment>
    {
        public UserAppointmentsMap()
        {
            CompositeId()
                .KeyReference(a => a.Appointment, "FkAppointmentsId")
                .KeyReference(u => u.User, "FkUserId");
            Table("UserAppointments");
        }
    }
    
  2. 带有外键的 Composite Id 应该使用 KeyReference 而不是 KeyProperty

  3. 我遇到错误生成查询的问题(外键错误,即它正在寻找 Appointments_Id 而不是 FkAppointmentsId)所以我必须明确指定列名

    public class AppointmentMap : ClassMap<Appointment>
    {
        public AppointmentMap()
        {
            Id(c => c.Id);
            HasMany(a => a.AppointmentParticipants).Inverse().KeyColumn("FkAppointmentsId").Cascade.All();
            References(a => a.Location).Column("FkAddressId");
            Map(a => a.DateAndTime).Column("AppointmentDate").CustomType<UtcDateTimeType>();
            Map(a => a.Description).Column("AppointmentDescription");
            Map(a => a.Status).Column("AppointmentStatus").CustomType<AppointmentStatus>();
            HasMany(a => a.AppointmentEstates).Inverse().KeyColumn("FkAppointmentsId").Cascade.All();
            Table("Appointments");
        }
    }