连接映射内的 HasMany 关系
所以,我在流畅的 nhibernate 中映射时遇到问题。我想使用联接映射来展平中间表: 这是我的结构:
[Vehicle]
VehicleId
...
[DTVehicleValueRange]
VehicleId
DTVehicleValueRangeId
AverageValue
...
[DTValueRange]
DTVehicleValueRangeId
RangeMin
RangeMax
RangeValue
请注意,DTValueRange 没有 VehicleID。我想将 DTVehicleValueRange 展平到我的 Vehicle 类中。 Tgis 对于 AverageValue 工作得很好,因为它只是一个普通值,但我似乎无法正确映射 ValueRange 集合。
public VehicleMap()
{
Id(x => x.Id, "VehicleId");
Join("DTVehicleValueRange", x =>
{
x.Optional();
x.KeyColumn("VehicleId");
x.Map(y => y.AverageValue).ReadOnly();
x.HasMany(y => y.ValueRanges).KeyColumn("DTVehicleValueRangeId"); // This Guy
});
}
如果 HasMany 映射位于 Join 内部,则它似乎不会执行任何操作。如果它位于 Join 之外并且我指定了表,它会进行映射,但 nhibernate 会尝试使用 VehicleID,而不是 DTVehicleValueRangeId。
我做错了什么?
So, I'm having a problem mapping in fluent nhibernate. I want to use a join mapping to flatten an intermediate table: Here's my structure:
[Vehicle]
VehicleId
...
[DTVehicleValueRange]
VehicleId
DTVehicleValueRangeId
AverageValue
...
[DTValueRange]
DTVehicleValueRangeId
RangeMin
RangeMax
RangeValue
Note that DTValueRange does not have a VehicleID. I want to flatten DTVehicleValueRange into my Vehicle class. Tgis works fine for AverageValue, since it's just a plain value, but I can't seem to get a ValueRange collection to map correctly.
public VehicleMap()
{
Id(x => x.Id, "VehicleId");
Join("DTVehicleValueRange", x =>
{
x.Optional();
x.KeyColumn("VehicleId");
x.Map(y => y.AverageValue).ReadOnly();
x.HasMany(y => y.ValueRanges).KeyColumn("DTVehicleValueRangeId"); // This Guy
});
}
The HasMany mapping doesn't seem to do anything if it's inside the Join. If it's outside the Join and I specify the table, it maps, but nhibernate tries to use the VehicleID, not the DTVehicleValueRangeId.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
今天使用地图创建视图时遇到了类似的问题。生成的 SQL 显示它尝试执行 HasMany<> 操作。在基于 ParentThing 而不是 WorkThing 的 Id 的连接内部(与您遇到的问题相同)
经过多次头到桌映射后,结果将 propertyref 添加到 hasmany 上解决了这个问题。
Ran into a similar issue today using a Map to create a view. The SQL generated showed it trying to do the HasMany<> inside the join based on the Id of the ParentThing and not WorkThing (same problem you were having)
After much mapping of head-to-desk it turns out adding the propertyref onto the hasmany solved it.
您能否解释一下
DTVehicleValueRange
表中的平均值列?这不是一个计算值(即不需要保留它)吗?看起来
Vehicle
和DTValueRange
之间存在多对多关系,当然不会使用联接进行映射,而是使用HasManyToMany< /代码> 调用。
Can you explain the average value column in the
DTVehicleValueRange
table? Isn't this a calculated value (i.e. no need to persist it)?It looks like you have a many-to-many relationship between
Vehicle
andDTValueRange
, which of course would not be mapped with a join, rather with aHasManyToMany
call.