這裏你需要的是一個與你的PlayerBowlerType一起使用的合成id。這樣的設置應該工作:
public class PlayerBowlerTypeId
{
public virtual int PlayerId { get; set; }
public virtual int BowlerTypeId { get; set; }
public override bool Equals(object obj)
{
return Equals(obj as PlayerBowlerTypeId);
}
private bool Equals(PlayerBowlerTypeId other)
{
if (ReferenceEquals(other, null)) return false;
if (ReferenceEquals(this, other)) return true;
return PlayerId == other.PlayerId &&
BowlerTypeId == other.BowlerTypeId;
}
public override int GetHashCode()
{
unchecked
{
int hash = GetType().GetHashCode();
hash = (hash * 31)^PlayerId.GetHashCode();
hash = (hash * 31)^BowlerTypeId.GetHashCode();
return hash;
}
}
}
public class PlayerBowlerType
{
public PlayerBowlerType()
{
Id = new PlayerBowlerTypeId();
}
public virtual PlayerBowlerTypeId Id { get; set; }
}
public class PlayerBowlerTypeMap : ClassMap<PlayerBowlerType>
{
public PlayerBowlerTypeMap()
{
Table("TABLENAME");
CompositeId<PlayerBowlerTypeId>(x => x.Id)
.KeyProperty(x => x.BowlerTypeId, "COLUMNNAME")
.KeyProperty(x => x.PlayerId, "COLUMNNAME");
}
}
技術上可以做到這一點沒有身份對象(PlayerBowlerTypeId類型將被移除,並直接將代碼放入PlayerBowlerType並適當調整),但我有一些問題(3-4個獨立的錯誤)。其中一個討論here。
雖然我討厭更改域對象來補償ORM系統中的錯誤,但如果您只是使用PlayerBowlerTypeId類型,它將爲您節省很多麻煩。
只要您修改映射以使用實際的表名和列名(以及您需要爲特定設置映射所做的任何其他操作),就應該可以工作。