Castle ActiveRecord HasAndBelongsToMany 问题
我正在使用 monorail/activerecord,并且想知道当多对多表中除了 2 个外键之外还有一个值时,如何处理在多对多关系中添加项目。
例如,Business 和 Amenity 类具有多对多关系,因此存在一个 BusinessAmenity 表。如果 BusinessAmenity 表只有外键 BusinessId 和 AmenityId,那么您可以执行以下操作:
[HasAndBelongsToMany(typeof(Amenity),
Table = "BusinessAmenity", ColumnKey = "businessid", ColumnRef = "amenityid", Cascade = ManyRelationCascadeEnum.None, Lazy=true)]
public IList<Amenity> Amenities
{
get { return _amenities; }
set { _amenities = value; }
}
然后添加如下所示的关联:
business.Amenities.Add(amenity;
但是,如果 BusinessAmenity 类还有另一个名为“Value”的列需要为每个关联设置,该怎么办?您无法再将 Amenity 对象添加到 Business.Amenities,因为您需要能够在 BusinessAmenity 中设置 Value 属性。
有人可以提供一些关于如何在 ActiveRecord 中执行此操作的见解吗?
谢谢! 贾斯汀
I'm using monorail/activerecord and was wondering how you handle adding items in a many to many relationship when the many to many table has a value in it besides just the 2 foreign keys.
For example, Business and Amenity classes have a many to many relationship so there is a BusinessAmenity table. If the BusinessAmenity table only had the foreign keys BusinessId and AmenityId then you could do this:
[HasAndBelongsToMany(typeof(Amenity),
Table = "BusinessAmenity", ColumnKey = "businessid", ColumnRef = "amenityid", Cascade = ManyRelationCascadeEnum.None, Lazy=true)]
public IList<Amenity> Amenities
{
get { return _amenities; }
set { _amenities = value; }
}
And then add the associations like this:
business.Amenities.Add(amenity;
However, what if the BusinessAmenity class has another column called "Value" that needs to be set for each association? You can no longer add an Amenity object to Business.Amenities because what you need to be able to set the Value property in BusinessAmenity.
Can someone provide some insight into how you do this in ActiveRecord?
Thanks!
Justin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 BusinessAmenity 映射到其自己的 BusinessAmenity 类,例如(伪代码):
这已在 stackoverflow 上讨论过多次:
Map the BusinessAmenity to its own BusinessAmenity class, e.g (pseudocode):
This has been discussed many times on stackoverflow: