创建SessionFactory时使用了无效或不完整的配置

use*_*336 5 nhibernate fluent-nhibernate

这是最后的内部异常:

无法加载文件或程序集"ByteCode.Castle"或其依赖项之一.该系统找不到指定的文件.

我正在为nhibernate添加所有引用,这里使用的所有构建都是我的代码:

使用NHibernate; 使用FluentNHibernate; 使用NHibernate.Cfg; 使用System.Reflection; 使用FluentNHibernate.Cfg.Db; 使用FluentNHibernate.Cfg; 使用NHibernate.ByteCode.Castle; 使用Castle.Core; 使用Castle.DynamicProxy;

namespace _3adaseh {public static class NHibernateHelper {private static void ReferByteCode(){//只是为了确保ByteCodeCastle被加载了ProxyFactory fake = new ProxyFactory(); }

    #region Session
    private static ISessionFactory _sessionFactory;

    private static ISessionFactory SessionFactory
    {
        get
        {
            if (_sessionFactory == null)
            {
                ReferByteCode();
                var configuration = new Configuration();
                #region Configuring Fluent NHibernate
                IPersistenceConfigurer persistenceConfigurer = MsSqlConfiguration.MsSql2008.ConnectionString("Data Source=.;Initial Catalog=3adaseh;Integrated Security=True").ShowSql().ProxyFactoryFactory("ByteCode.Castle.ProxyFactoryFactory, ByteCode.Castle");
                //
                // initialize nhibernate with persistance configurer properties 
                //Configuration cfg = persistenceConfigurer.ConfigureProperties(new Configuration());
                //var persistenceModel = new PersistenceModel();
                //persistenceModel.AddMappingsFromAssembly(Assembly.Load("3adaseh.Mappings"));
                //persistenceModel.Configure(cfg);
                try
                {
                    _sessionFactory = Fluently.Configure().Database(persistenceConfigurer).Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.Load("3adaseh.Mappings"))).BuildSessionFactory();
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }

                //cfg.SetProperty(
                // add mappings definition to nhibernate configuration 
                //try
                //{
                //    var persistenceModel = new PersistenceModel();
                //    persistenceModel.AddMappingsFromAssembly(Assembly.Load("3adaseh.Mappings"));
                //    persistenceModel.Configure(cfg);
                //    _sessionFactory = configuration.BuildSessionFactory();
                //}
                //catch (System.Exception ex)
                //{
                //    throw ex;
                //}
                  #endregion




            }
            return _sessionFactory;
        }
    }

    public static ISession OpenSession()
    {
        return SessionFactory.OpenSession();
    }
    #endregion

    #region CRUD Operations
    public static void Add<T>(T newObject)
    {
        using (ISession session = NHibernateHelper.OpenSession())
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Save(newObject);
                transaction.Commit();
            }
        }
    }


    public static void Update<T>(T updatedObject)
    {
        using (ISession session = NHibernateHelper.OpenSession())
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Update(updatedObject);
                transaction.Commit();
            }
        }
    }

    public static void Remove<T>(T deletedObject)
    {
        using (ISession session = NHibernateHelper.OpenSession())
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Delete(deletedObject);
                transaction.Commit();
            }
        }
    }

    public static T GetById<T>(int objectID)
    {
        using (ISession session = NHibernateHelper.OpenSession())
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                return session.Get<T>(objectID);
            }
        }
    }
    #endregion
}
Run Code Online (Sandbox Code Playgroud)

}

到目前为止我无法测试任何东西,我真的厌倦了这个错误,我添加了对所有类库的nhibernate引用,没有任何修复,有人可以帮助吗?

Jam*_*acs 2

确保您具有对 NHibernate.ByteCode.Castle.dll 和 Castle.Core.dll(以及 Castle.DynamicProxy2.dll,如果您使用 NH2.1.2)* 的程序集引用,以确保将其复制到输出目录中。您使用的是 Fluent NHibernate 和 NHibernate 的哪个版本?

* Castle.DynamicProxy2.dll 已与 Castle.Core.dll 合并。NH3 中使用了 Castle.Core.dll 的较新合并版本。