c# – NHibernate Evict按类型而不是实例
发布时间:2020-12-15 07:58:58 所属栏目:百科 来源:网络整理
导读:我正在迁移这样的应用程序: Vehicle v = null;using (ISession session = MyNHibernateSession()){ v = Vehicle.FindById(1);}using (ISession session = MyNHibernateSession()){ // somwwhere into these4 lines Vehicle comes Finded DoSomething(); DoS
我正在迁移这样的应用程序:
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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |