您需要的是与您的 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 个单独的错误)。这里讨论其中之一。
虽然我讨厌更改域对象以弥补 ORM 系统中的错误,但如果您只使用 PlayerBowlerTypeId 类型,它将为您省去很多麻烦。
只要您修改映射以使用您的实际表和列名称(以及您需要对特定设置的映射执行的任何其他操作),这应该可以工作。