7

私は次のようなアプリケーションを移行しています:

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

出来ますか?どのように?、ありがとう

4

2 に答える 2

7

この質問は古いかもしれませんが、その方法を探しているときにここにたどり着きました。だから、これは私がそれをやった方法です:

    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;
    }
于 2012-07-23T21:14:10.640 に答える
0

私見 v は2番目のセッションに属していないため、エビクトはあなたの場合の解決策ではないと思います(したがって、すべての車両をエビクトするだけでは十分ではありません)。

私の提案は、次のように v を 2 番目のセッションにアタッチすることです。

...
using (ISession session = MyNHibernateSession())
{
     session.Lock(v, LockMode.None);

     // somwwhere into these4 lines Vehicle comes Finded
...
于 2012-03-05T18:05:53.157 に答える