我有一個Store
包含的Products
列表:如何保存一個已存在於NHibernate會話中的瞬態對象?
var store = new Store();
store.Products.Add(new Product{ Id = 1, Name = "Apples" };
store.Products.Add(new Product{ Id = 2, Name = "Oranges" };
Database.Save(store);
現在,我想編輯的Products
之一,但有短暫的實體。這將是,例如,從網絡瀏覽器數據:
// this is what I get from the web browser, this product should
// edit the one that's already in the database that has the same Id
var product = new Product{ Id = 2, Name = "Mandarin Oranges" };
store.Products.Add(product);
Database.Save(store);
然而,試圖做這種方式給我一個錯誤:
a different object with the same identifier value was already associated with the session
原因是因爲store.Products
集合已經包含具有相同Id的實體。我如何解決這個問題?
你有保存第二次受審session.Evict(產品)? – mxmissile 2010-04-24 08:24:02