在 Castle ActiveRecord 中手动管理联接表
我有两个连接的类定义如下:
[ActiveRecord]
public class Store : ActiveRecordBase<Store> {
[PrimaryKey]
public int ID { get; set; }
[HasAndBelongsToMany(Table = "StoreCustJoin",
ColumnKey = "storeID",
ColumnRef = "customerID")]
public IList<Customer> customers { get; set; }
}
[ActiveRecord]
public class Customer : ActiveRecordBase<Customer> {
[PrimaryKey]
public int ID { get; set; }
[HasAndBelongsToMany(Table = "Join",
ColumnKey = "customerID",
ColumnRef = "storeID",
inverse = true)]
public IList<Store> stores { get; set; }
}
[ActiveRecord]
public class Join : ActiveRecordBase<Join> {
[PrimaryKey]
public int ID { get; set; }
[BelongsTo("storeID")]
public Store store { get; set; }
[BelongsTo("customerID")]
public Customer customer { get; set; }
[Property]
public int Status { get; set; }
}
当我将商店连接到客户时,我还需要设置状态。所以我尝试这样做:
public void SetLink(Store theStore, Customer theCustomer, int status) {
var link = new Join()
{
Status = status,
store = theStore,
customer = theCustomer
};
theStore.customers.Add(theCustomer);
theCustomer.stores.Add(theStore);
}
这会在 ABJoin 中创建两个条目,第一个具有状态集,第二个没有。如果我不将对象添加到各自的列表中,它就会起作用。但是,直到当前会话关闭并从数据库重新下载实例之前,链接不会反映在这些对象中。
那么,有没有一种方法可以在创建链接时设置状态并保持当前对象有效且最新?
I have two connected classes defined like this:
[ActiveRecord]
public class Store : ActiveRecordBase<Store> {
[PrimaryKey]
public int ID { get; set; }
[HasAndBelongsToMany(Table = "StoreCustJoin",
ColumnKey = "storeID",
ColumnRef = "customerID")]
public IList<Customer> customers { get; set; }
}
[ActiveRecord]
public class Customer : ActiveRecordBase<Customer> {
[PrimaryKey]
public int ID { get; set; }
[HasAndBelongsToMany(Table = "Join",
ColumnKey = "customerID",
ColumnRef = "storeID",
inverse = true)]
public IList<Store> stores { get; set; }
}
[ActiveRecord]
public class Join : ActiveRecordBase<Join> {
[PrimaryKey]
public int ID { get; set; }
[BelongsTo("storeID")]
public Store store { get; set; }
[BelongsTo("customerID")]
public Customer customer { get; set; }
[Property]
public int Status { get; set; }
}
When I connect a Store to a Customer I need to set the status as well. So I tried to do this:
public void SetLink(Store theStore, Customer theCustomer, int status) {
var link = new Join()
{
Status = status,
store = theStore,
customer = theCustomer
};
theStore.customers.Add(theCustomer);
theCustomer.stores.Add(theStore);
}
This creates two entries in ABJoin, the first with the status set, the second without. If I don't add the objects to their respective lists, it works. But then the link is not reflected in these objects until the current session is closed and the instance is re-downloaded from the DB.
So, is there a way I can set the Status when I create the link and also keep the current objects valid and up to date?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您的关系表(在您的情况下为“Join”表)除了与其关联的表的 FK 之外还有其他字段时,请使用
[HasMany]
而不是[HasAndBelongsToMany]
,即:类似问题:
When your relationship table (in your case the "Join" table) has additional fields other than the FKs to the tables it relates, use
[HasMany]
instead of[HasAndBelongsToMany]
, i.e.:Similar questions: