我有桌子:
播放器
Id : int -主键
名称:字符串
BowlerType
Id : int -主键
描述:字符串
PlayerBowlerType
PlayerId : int非空外键引用Player.Id
BowlerTypeId : int非空外键引用BowlerType.Id
一个球员可以确定许多保龄球类型。以下是一些示例数据
播放器
1名Peter
2. John
BowlerType
6%慢
7.快速
PlayerBowlerType
1/6
1/2-7
2/2-7
发布于 2011-12-11 18:47:42
这里需要的是一个与您的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个单独的bug)。其中之一是讨论here。
虽然我讨厌更改域对象以弥补ORM系统中的错误,但是如果您只使用PlayerBowlerTypeId类型,它将为您节省很多麻烦。
只要您修改映射以使用实际的表名和列名(以及您对特定设置的映射需要做的其他任何事情),这就可以工作。
发布于 2013-08-17 11:02:39
我想我们可以使用HasManytoMany。根据您的需求,您必须创建一个包含玩家ids和保龄球类型的表。这有很多到很多关系。
如果您查看这个站点:https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started,商店和产品的映射与您预期的映射相同。
https://stackoverflow.com/questions/8456806
复制相似问题