How do I save a transient object that already exists in an NHibernate session?

Posted by Daniel T. on Stack Overflow See other posts from Stack Overflow or by Daniel T.
Published on 2010-04-24T07:42:09Z Indexed on 2010/04/24 7:43 UTC
Read the original article Hit count: 268

I have a Store that contains a list of Products:

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);

Now, I want to edit one of the Products, but with a transient entity. This will be, for example, data from a web browser:

// 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);

However, trying to do it this way gives me an error:

a different object with the same identifier value was already associated with the session

How do I get around this problem?

© Stack Overflow or respective owner

Related posts about castle-activerecord

Related posts about nhibernate