在 Castle ActiveRecord 中手动管理联接表

发布于 2024-09-28 20:05:38 字数 1765 浏览 1 评论 0原文

我有两个连接的类定义如下:

    [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 技术交流群。

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

发布评论

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

评论(1

愚人国度 2024-10-05 20:05:38

当您的关系表(在您的情况下为“Join”表)除了与其关联的表的 FK 之外还有其他字段时,请使用 [HasMany] 而不是 [HasAndBelongsToMany] ,即:

[ActiveRecord]
public class Store : ActiveRecordBase<Store> {
    [PrimaryKey]
    public int ID { get; set; }

    [HasMany]
    public IList<Join> customers { get; set; }
}

[ActiveRecord]
public class Customer : ActiveRecordBase<Customer> {
    [PrimaryKey]
    public int ID { get; set; }

    [HasMany]
    public IList<Join> stores { get; set; }
}

类似问题:

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.:

[ActiveRecord]
public class Store : ActiveRecordBase<Store> {
    [PrimaryKey]
    public int ID { get; set; }

    [HasMany]
    public IList<Join> customers { get; set; }
}

[ActiveRecord]
public class Customer : ActiveRecordBase<Customer> {
    [PrimaryKey]
    public int ID { get; set; }

    [HasMany]
    public IList<Join> stores { get; set; }
}

Similar questions:

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