我剛開始工作,作爲一名開發人員,我一遍又一遍地遇到同樣的問題。我不斷收到此錯誤:NHibernate配置錯誤
Exception (SessionFactory): An exception occurred during configuration of persistence layer./ em NHibernate.Cfg.ConfigurationSchema.HibernateConfiguration..ctor(XmlReader hbConfigurationReader, Boolean fromAppSetting) em NHibernate.Cfg.Configuration.Configure(XmlReader textReader) em NHibernate.Cfg.Configuration.Configure(String fileName, Boolean ignoreSessionFactoryConfig) em DataLayer.Repository.NHibernateHelper.get_SessionFactory() at 14/12/2016 16:52:52 0 PCService Error on: 14/12/2016 16:52:52 Referência de objeto não definida para uma instância de um objeto. em DataLayer.Repository.ConfigRobotRepository.FindByName(String name) em RSPC.PCService.ScheduleService() at line 0
我的hibernate.cfg.xml:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</property>
<property name="dialect">
NHibernate.Dialect.MySQLDialect
</property>
<property name="connection.driver_class">
NHibernate.Driver.MySqlDataDriver
</property>
<property name="connection.connection_string">
Server=localhost; Database=bd; Uid=ti; Pwd=************;
</property>
<property name="show_sql">false</property>
</session-factory>
</hibernate-configuration>
我NHibernateHelper.cs:
public class NHibernateHelper
{
private static ISessionFactory _sessionFactory;
private static ISessionFactory SessionFactory
{
get
{
try
{
if (_sessionFactory == null)
//CreateSessionFactory();
{
var configuration = new Configuration();
//configuration.SetProperty(NHibernate.Cfg.Environment.ConnectionString, ConnectionString);
configuration.Configure("hibernate.cfg.xml");
configuration.AddAssembly(typeof(ConfigRobot).Assembly);
_sessionFactory = configuration.BuildSessionFactory();
}
return _sessionFactory;
}
catch (Exception e)
{
System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(e, true);
var frame = trace.GetFrame(0);
var line = frame.GetFileLineNumber();
Log("Exception (SessionFactory): " + e.Message + "/" + e.StackTrace + " at {0} " + line, Path.GetTempPath() + "PCService" + ".txt");
return null;
}
}
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
任何幫助,非常感謝!謝謝!
這是您在調試時?確保你的NHibernate配置設置爲'Copy Always',這樣當你生成/運行時它就會在Debug和Release文件夾中結束。 – TyCobb
你發佈了你的密碼? :) –
@KeithNicholas剛剛編輯過,工作了這麼多小時後,我甚至沒有注意到它哈哈 –