मैं एक 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
संग्रह पहले से ही शामिल है एक एक ही आईडी के साथ इकाई। मैं इस समस्या के आसपास कैसे हो सकता हूं?
आप दूसरी बार सहेजने से पहले session.Evict (उत्पाद) की कोशिश की? – mxmissile