2010-10-20 86 views
0

连接表我有这样定义了两个连接类:手动管理在城堡的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中创建两个条目,第一个是状态集,第二个没有。如果我不将这些对象添加到它们各自的列表中,它就会起作用。但是,直到当前会话关闭并且实例从数据库重新下载之后,链接才会反映在这些对象中。

那么,有没有一种方法可以在创建链接时设置状态,还可以使当前对象保持有效并保持最新状态?

回答

1

当你的关系表(在你的情况下,“加入”表)具有比FKS它涉及表等附加字段,使用[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; } 
} 

类似的问题:

+0

谢谢。这对我很有帮助。 – Jacobi 2015-04-28 20:48:03