我正在迁移这样的应用程序:
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;
}