本文介绍了无Persister:与INotifyPropertyChanged拦截器保存时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用,仓库将返回一个实现INotifyPropertyChanged的代理集合,但是在保存或删除它的时候会抛出一个错误:

在NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(字符串实体名称)$ $ b $在NHibernate.Impl.SessionImpl.GetEntityPersister(字符串entityName,对象obj)
在NHibernate.Engine.ForeignKeys.IsTransient String entityName,Object entity,Nullable`1 assume,ISessionImplementor session)
at NHibernate.Event.Default.AbstractSaveEventListener.GetEntityState(Object entity,String entityName,EntityEntry entry,ISessionImplementor source)
at NHibernate.Event。在NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent事件)
NHibernate.Impl.SessionImpl.FireSaveOrUpdate(SaveOrUpdateEvent事件)
在NHibernate
.Impl.SessionImpl.SaveOrUpdate(Object obj)$ b $ MyCode ...

如果我在没有拦截器的情况下创建会话,SaveOrUpdate工作正常,但是拦截器会出错。

用拦截器:

  public ISession GetSession(ISessionFactory factory)
{
IInterceptor dataBinding = new DataBindingInterceptor {SessionFactory = factory};
返回factory.OpenSession(dataBinding);

$ / code>


<$ p公共ISession GetSession(ISessionFactory工厂)
{
返回factory.OpenSession();

$ / code $


我不知如何去解决为什么拦截器会中断保存。



我对代码进行的唯一更改是更改行



类型类型= Type.GetType(clazz);

to



类型type = FindType(clazz);

  public Type FindType(string typeName)
{
foreach(AppDomain.CurrentDomain.GetAssemblies()
{
类型foundType = assembly.GetType(typeName);

if(foundType!= null)
return foundType;
}
返回null;


解决方案

解决方法是始终使用与拦截器会话。我用拦截器创建了IList,但是保存了一个通用的会话。这绕过了将代理重定向到正确的持久存储的GetEntityName覆盖。


Using the method described in NHibernate & INotifyPropertyChanged, the repository will return a collection of proxies that implement INotifyPropertyChanged, but on some objects when saving or deleting it will throw an error:

   at NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(String entityName)
   at NHibernate.Impl.SessionImpl.GetEntityPersister(String entityName, Object obj)
   at NHibernate.Engine.ForeignKeys.IsTransient(String entityName, Object entity, Nullable`1 assumed, ISessionImplementor session)
   at NHibernate.Event.Default.AbstractSaveEventListener.GetEntityState(Object entity, String entityName, EntityEntry entry, ISessionImplementor source)
   at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event)
   at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
   at NHibernate.Impl.SessionImpl.FireSaveOrUpdate(SaveOrUpdateEvent event)
   at NHibernate.Impl.SessionImpl.SaveOrUpdate(Object obj)
   at MyCode ...

I figured out that if I create the session without the interceptor the SaveOrUpdate works fine, but with the interceptor it errors.

with the interceptor:

 public ISession GetSession(ISessionFactory factory)
        {
            IInterceptor dataBinding = new DataBindingInterceptor {SessionFactory = factory};
            return factory.OpenSession(dataBinding);
        }

without

 public ISession GetSession(ISessionFactory factory)
        {
            return factory.OpenSession();
        }

I'm at a loss for how to go about even figuring out why the interceptor would break the save.

The only change I made to the code was changing the line

Type type = Type.GetType(clazz);

to

Type type = FindType(clazz);

public Type FindType(string typeName)
{
    foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
    {
        Type foundType = assembly.GetType(typeName);

        if (foundType != null)
            return foundType;
    }
    return null;
}
解决方案

Solution was to always use the session with the interceptor. I was creating the IList with the interceptor, but saving with a generic session. This bypassed the GetEntityName override which redirected the proxy to the correct persister.

这篇关于无Persister:与INotifyPropertyChanged拦截器保存时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 03:23