首页 新闻 赞助 找找看

hibernate.cfg.xml配置文件错误的怪问题:不能包含文本

0
悬赏园豆:30 [已关闭问题]

今天下载了NHibernate-2.1.2.GA-bin.zip版本,做了一个简单的例子,发现了2个怪问题,一直找不到原因,请大家帮忙看看:

例子代码如下:

代码
Configuration cfg = new Configuration();

IDictionary
<string, string> props = new Dictionary<string, string>();
props.Add(
"hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
props.Add(
"hibernate.connection.driver_class", "NHibernate.Driver.SqlClientDriver");
props.Add(
"hibernate.connection.connection_string", @"Server=.\SQLExpress;Initial Catalog=Test;Integrated Security=SSPI");
props.Add(
"show_sql", "true");
props.Add(
"hibernate.dialect", "NHibernate.Dialect.MsSql2005Dialect");

cfg.Properties
= props;
cfg.Configure();

ISessionFactory factory
= cfg.BuildSessionFactory();
session
= factory.OpenSession();
Console.WriteLine(session);


1.首先是想通过硬编码的方式设置数据库连接等信息的,但是一直报找不到hibernate.cfg.xml配置文件的错误,程序中"cfg.Properties = props;"已经设置了配置信息,为什么还要找hibernate.cfg.xml配置文件?

详细异常信息如下:
NHibernate.Cfg.HibernateConfigException : An exception occurred during configuration of persistence layer.
  ----> System.IO.FileNotFoundException : 未能找到文件“...\bin\Debug\hibernate.cfg.xml”。
 
2.后来我直接编写了hibernate.cfg.xml文件放到项目的bin\Debug下,这时候又报另外一个错误说"不能包含文本。应为可能元素的列表",一直找不到原因,最后我直接把hibernate.cfg.xml里的空格都去掉,按F5运行又正常了,奇怪,为什么没有空格的配置文件就可以呢?

说明:本文最下面的截图里第一个是运行有错误的hibernate.cfg.xml,第二个是运行正常的没有空格的hibernate.cfg.xml

详细异常信息如下:
NHibernate.Cfg.HibernateConfigException : An exception occurred parsing configuration :元素 命名空间“urn:nhibernate-configuration-2.2”中的“hibernate-configuration”。 不能包含文本。应为可能元素的列表: 命名空间“urn:nhibernate-configuration-2.2”中的“bytecode-provider, reflection-optimizer, session-factory”。。
  ----> System.Xml.Schema.XmlSchemaValidationException : 元素 命名空间“urn:nhibernate-configuration-2.2”中的“hibernate-configuration”。 不能包含文本。应为可能元素的列表: 命名空间“urn:nhibernate-configuration-2.2”中的“bytecode-provider, reflection-optimizer, session-factory”。
 
运行有错误的hibernate.cfg.xml:

运行正常的没有空格的hibernate.cfg.xml:

uncletwo的主页 uncletwo | 初学一级 | 园豆:100
提问于:2010-01-03 07:59
< >
分享
其他回答(1)
0

至于你上面的原因,我猜想是NHibernate硬编码之后不能正常解析hibernate.cfg.xml文件。

cfg.Configure();默认就是找hibernate.cfg.xml文件解析。

既然你写了硬编码,就不要用hibernate.cfg.xml文件了,就是不用调用cfg.Configure();

当然最好使用hibernate.cfg.xml文件,注意把hibernate.cfg.xml文件属性设置为“始终复制”。

Configuration cfg = new Configuration();
cfg.Configure();
ISessionFactory factory
= cfg.BuildSessionFactory();
session
= factory.OpenSession();

就可以了。

李永京 | 园豆:3114 (老鸟四级) | 2010-01-03 13:20
0

我也遇到这么问题  ? 是怎么解决的呀 ?

韩梦芫 | 园豆:242 (菜鸟二级) | 2014-08-11 08:57
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册