0
我使用的是NHibernate 3.2,並使用按代碼映射。在nhibernate中收集級聯類型
測試用例非常簡單 - 我有一個商店和一個產品,通過多對多的集合鏈接。許多一對多的集合中的StoreMap映射如下:
Set(x => x.Products, x =>
{
x.Fetch(CollectionFetchMode.Subselect);
x.Key(key =>
{
key.Column("StoreId");
key.ForeignKey("FK_StoreProducts_Store");
});
x.Table("StoreProducts");
x.Cascade(Cascade.None);
x.Inverse(true);
},
如果我是正確的,級聯選項應該可以讓你選擇是否要級聯的類業務,集合,以及。 NHibernate的3.2允許這些選項,這是不是真的簡單的對我說:
[Flags]
public enum Cascade
{
None = 0,
Persist = 2,
Refresh = 4,
Merge = 8,
Remove = 16,
Detach = 32,
ReAttach = 64,
DeleteOrphans = 128,
All = 256,
}
我使用的例子是下面的一個,在那裏我創建一個測試存儲和測試產品,並將其分配給商店。然後我重新加載商店,檢索第一個產品,更新它並保存商店。
即使級聯設置爲「無」,產品仍會更新!這是否正常?你正面臨
using (var session = SessionFactory.OpenSession())
{
long storeId = 5;
using (var t = session.BeginTransaction())
{
Product p1 = new Product();
p1.Name = "Product 1";
session.Save(p1);
Store store = new Store();
store.Name = "Tesco";
session.Save(store);
p1.Stores.Add(store);
store.Products.Add(p1);
session.Save(store);
storeId = store.Id;
t.Commit();
}
using (var t = session.BeginTransaction())
{
Store s = session.Get<Store>(storeId);
Product p = s.Products.FirstOrDefault();
p.Name = "Product 1 Updated";
session.Save(s);
t.Commit(); // <-- Product is still being updated here!
}
session.Flush();
}
那麼只有級聯刪除的'Cascade'選項是什麼?它可以提供什麼其他目的? –
刪除和保存,但它只關於收集成員,而不是每個成員屬性 – Sly