【发布时间】:2014-02-16 12:42:35
【问题描述】:
我在 sql 中有很多对很多,我正在尝试使用 nhibernate 映射它。 在努力走到这一步之后,我现在已经接近了,但我的 nhibernate 创建的 sql 字符串中有一个问号,如下所示。我错过了什么吗?
我需要在每个类中创建一个 add 方法来添加另一个实体吗?
谢谢,
我的问题——fluent nhibernate产生的sql字符串
... y=productcat1_.itm_Key WHERE productcat0_.itr_Key=?
对象 A
public class Range : IEntity
{
public virtual IList<Item> Items { get; set; }
}
对象 B
public class Item : IEntity
{
public virtual IList<Range> Ranges { get; set; }
}
映射
// Item mapping
mapping.HasManyToMany(x => x.Ranges)
.Table("itr_RangeItemsAssoc")
.ParentKeyColumn("itm_Key")
.ChildKeyColumn("itr_ItemsKey")
.Cascade.SaveUpdate().LazyLoad();
// Range mapping
mapping.HasManyToMany(x => x.Items)
.Table("itr_RangeItemsAssoc")
.ParentKeyColumn("itr_Key")
.ChildKeyColumn("itr_ItemRangeKey")
.Cascade.SaveUpdate().LazyLoad();
更新
已将以下方法添加到我的实体中,尽管我没有在任何地方调用它们..?
public class Range : IEntity
{
....
public virtual void AddItem(Item item)
{
item.Ranges.Add(this);
Items.Add(item);
}
}
public class Item : IEntity
{
...
public virtual void AddRange(Range range)
{
range.Items.Add(this);
Ranges.Add(range);
}
}
更新 2 - 映射更正
mapping.HasManyToMany(x => x.Ranges)
.Table("itr_RangeItemsAssoc") // name of the look up table
.ParentKeyColumn("itr_ItemsKey") // key for item in the lookup table
.ChildKeyColumn("itm_Key") // key column in the item table
.Cascade.SaveUpdate().LazyLoad().Inverse();
}
mapping.HasManyToMany(x => x.Items)
.Table("itr_RangeItemsAssoc") // name of the look up table
.ParentKeyColumn("itr_ItemRangeKey") // key for the range in the lookup tablei
.ChildKeyColumn("itr_Key") // key column in the range table
.Cascade.SaveUpdate().LazyLoad();
非常感谢,
【问题讨论】:
标签: c# nhibernate fluent-nhibernate