我正在迁移这样的应用程序:
Vehicle v = null; using (ISession session = MyNHibernateSession()) { v = Vehicle.FindById(1); } using (ISession session = MyNHibernateSession()) { // somwwhere into these4 lines Vehicle comes Finded DoSomething(); DoSomething2(); DoSomething3(); DoSomething4(); DoSomething5(); DoSomething6(); // if i do this i get an error "another object with the same id etc etc etc session.Update(v); }
我不想这样做:
session.EvictAllByType(typeof(Vehicle));
可能吗?怎么样?,
谢谢
解决方法
这个问题可能已经过时了,但我在搜索这个问题时最终到了这里.所以这就是我最终做到的方式:
public static void EvictAll<T>(this ISession session,Predicate<T> predicate = null) { if (predicate == null) predicate = x => true; foreach (var entity in session.CachedEntities<T>().Where(predicate.Invoke).ToArray()) session.Evict(entity); } public static IEnumerable<T> CachedEntities<T>(this ISession session) { var sessionImplementation = session.GetSessionImplementation(); var entities = sessionImplementation.PersistenceContext.EntityEntries.Keys.OfType<T>(); return entities; }