Ho un Store
che contiene un elenco di Products
:Come si salva un oggetto temporaneo già esistente in una sessione di 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);
Ora, voglio modificare uno dei Products
, ma con un'entità transitoria. Questo sarà, ad esempio, i dati da un browser web:
// 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);
Tuttavia, cercando di fare in questo modo mi dà un errore:
a different object with the same identifier value was already associated with the session
il motivo è perché la raccolta store.Products
contiene già un entità con lo stesso ID. Come faccio a risolvere questo problema?
Hai provato session.Evict (del prodotto) prima di salvare la seconda volta? – mxmissile