2010-10-20 83 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