流畅的NHibernate复合键映射和加载

时间:2014-06-12 08:46:47

标签: c# nhibernate fluent-nhibernate

这是我第一次映射复合键,因为我以前的项目之前从未有类似的东西。我需要所有高级程序员的指导,以正确的方式指导我,如果我以错误的方式做到这一点,请纠正我。 现在我有这样的表

Tbl_Room
PK int Id
varchar Name

Tbl_Room_Facility
PK int RoomID
PK int FacilityID
int Quantity

Tbl_Facility
PK int Id
varchar Name

创建类应该很简单,我编码就像这样

public class Room {
  public int Id {get; set;}
  public string Name {get; set;}
  public IList<RoomFacility> Facilities {get; set;}
}

public class RoomFacility {
  public Room Room {get; set;}
  public Facility Facility {get; set;}
  public int Quantity {get; set;}
}

public class Facility {
  public int Id {get; set;}
  public string Name {get; set;}
  public IList<RoomFacility> FacilitiesOf {get; set;}
}

现在这是我的地图类看起来像

internal sealed class RoomMap : ClassMap<Room>{
  public RoomMap() {
     Table("Tbl_Room");
     Id(f => f.Id).GeneratedBy.Native();
     Map(f => f.Name);
     HasMany(f => f.Facilities).KeyColumn("RoomID").Cascade.SaveUpdate();
  }
}

internal sealed class FacilityMap : ClassMap<Facility>{
  public FacilityMap() {
     Table("Tbl_Facility");
     Id(f => f.Id).GeneratedBy.Native();
     Map(f => f.Name);
     HasMany(f => f.Facilities).KeyColumn("FacilityID").Inverse().Cascade.SaveUpdate();
  }
}

internal sealed class RoomFacilityMap : ClassMap<RoomFacility>{
  public FacilityMap() {
     Table("Tbl_Room_Facility");
     CompositeId()
                .KeyReference(f => f.Room)
                .KeyReference(f => f.Facility);
     Map(f => f.Quantity);
     Reference(f => f.Room, "RoomID").Cascade.None();
     Reference(f => f.Facility, "FacilityID").Cascade.None();
  }
}

我是否为RoomFacilityMap类做错了?我如何从会话中获得RoomFacility类? 我应该像这个tutorial一样创建Serializeable类吗?

0 个答案:

没有答案