11

In my nhibenate session I Mapping object with AutoMapper and in the afterMap action i create new instance of the object because I extract the object from the DB for properties compare. So The AutoMapper create two instances of one object with the same ID. When I try to commit the session i get error that i have to object with the same ID.

So I Want to disconnect the object that I extract after the properties compare. After that I will can commit the session

How I do that?

Thanks!

jishi
  • 24,126
  • 6
  • 49
  • 75
user556882
  • 121
  • 1
  • 5

1 Answers1

13

You can use session.Evict(persistentObject) to evict a persistent object from the session. This will remove the object from the 1st level cache, thus allowing you to flush the session.

mookid8000
  • 18,258
  • 2
  • 39
  • 63
  • Is it possible to remove it from all sessions? – Joel Apr 11 '14 at 11:34
  • Unless you do something really really funky, your objects will be connected to at most one session: the one that hydrated it – mookid8000 Apr 11 '14 at 12:33
  • I really wish that was true. I've some legacy code that does funkier stuff, so I came across this exception, and I hoped there would be some hackish workaround, but oh well... – Joel Apr 11 '14 at 13:23