Unity + NHibernate 3.3.1.4和中等信任错误

时间:2012-09-23 09:51:41

标签: c# nhibernate fluent-nhibernate unity-container

在中等信任环境中使用Unity + Fluent NHibernate + NHibernate 3.3.1.4

var session = sessionFactory.OpenSession();

返回此错误:

Attempt by method 'NHibernate.AdoNet.SqlClientSqlCommandSet..ctor()' to access method 'System.Data.SqlClient.SqlCommandSet..ctor()' failed.

   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at NHibernate.AdoNet.SqlClientSqlCommandSet..ctor()
   at NHibernate.AdoNet.SqlClientBatchingBatcher.CreateConfiguredBatch()
   at NHibernate.AdoNet.SqlClientBatchingBatcher..ctor(ConnectionManager connectionManager, IInterceptor interceptor)
   at NHibernate.AdoNet.SqlClientBatchingBatcherFactory.CreateBatcher(ConnectionManager connectionManager, IInterceptor interceptor)
   at NHibernate.AdoNet.ConnectionManager..ctor(ISessionImplementor session, IDbConnection suppliedConnection, ConnectionReleaseMode connectionReleaseMode, IInterceptor interceptor)
   at NHibernate.Impl.SessionImpl..ctor(IDbConnection connection, SessionFactoryImpl factory, Boolean autoclose, Int64 timestamp, IInterceptor interceptor, EntityMode entityMode, Boolean flushBeforeCompletionEnabled, Boolean autoCloseSessionEnabled, ConnectionReleaseMode connectionReleaseMode)
   at NHibernate.Impl.SessionFactoryImpl.OpenSession(IDbConnection connection, Boolean autoClose, Int64 timestamp, IInterceptor sessionLocalInterceptor)
   at NHibernate.Impl.SessionFactoryImpl.OpenSession(IInterceptor sessionLocalInterceptor)
   at NHibernate.Impl.SessionFactoryImpl.OpenSession()
   at BsCms.Bootstrapper.<>c__DisplayClass5.<BuildUnityContainer>b__2(IUnityContainer c)
   at Microsoft.Practices.Unity.InjectionFactory.<>c__DisplayClass2.<.ctor>b__0(IUnityContainer c, Type t, String s)
   at Microsoft.Practices.ObjectBuilder2.FactoryDelegateBuildPlanPolicy.BuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

这是一个非常容易解决的问题。您必须禁用批处理。您可以通过将属性adonet.batch_size设置为0

来执行此操作

hibernate.cfg.xml / App.config的{​​{1}}或nhibernate配置部分中:

Web.config

或者是FluentNHibernate:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <reflection-optimizer use="false"/>
  <session-factory>
    <!-- other properties -->
    <property name="adonet.batch_size">0</property>
  </session-factory>
</hibernate-configuration>