Castle ActiveRecord HasAndBelongsToMany 问题

发布于 2024-09-06 10:46:48 字数 835 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余厌 2024-09-13 10:46:49

将 BusinessAmenity 映射到其自己的 BusinessAmenity 类,例如(伪代码):

[ActiveRecord]
class Business {
    [PrimaryKey] int Id {get;set;}
    [HasMany] ISet<BusinessAmenity> Amenities {get;set;}
}

[ActiveRecord]    
class Amenity {
    [PrimaryKey] int Id {get;set;}
    [HasMany] ISet<BusinessAmenity> Businesses {get;set;}
}

[ActiveRecord]    
class BusinessAmenity {
    [BelongsTo] Amenity Amenity {get;set;}
    [BelongsTo] Business Business {get;set;}
    [Property] int Value {get;set;}
}

这已在 stackoverflow 上讨论过多次:

Map the BusinessAmenity to its own BusinessAmenity class, e.g (pseudocode):

[ActiveRecord]
class Business {
    [PrimaryKey] int Id {get;set;}
    [HasMany] ISet<BusinessAmenity> Amenities {get;set;}
}

[ActiveRecord]    
class Amenity {
    [PrimaryKey] int Id {get;set;}
    [HasMany] ISet<BusinessAmenity> Businesses {get;set;}
}

[ActiveRecord]    
class BusinessAmenity {
    [BelongsTo] Amenity Amenity {get;set;}
    [BelongsTo] Business Business {get;set;}
    [Property] int Value {get;set;}
}

This has been discussed many times on stackoverflow:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文