gpt4 book ai didi

c# - NHibernate Evict By Type 而不是实例

转载 作者:太空狗 更新时间:2023-10-29 20:21:28 27 4
gpt4 key购买 nike

我正在迁移这样的应用程序:

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

关于c# - NHibernate Evict By Type 而不是实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9569863/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com