首页 新闻 会员 周边

提示测试方法找不到hibernate.cfg.xml

0
悬赏园豆:20 [已解决问题] 解决于 2011-12-30 09:06

小弟刚学Nhibernate 软件vs2008

TestProject2.UnitTest1.TestMethod1 引发异常: NHibernate.Cfg.HibernateConfigException: An exception occurred during configuration of persistence layer. ---> System.IO.DirectoryNotFoundException: 未能找到路径“c:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PublicAssemblies;PrivateAssemblies\hibernate.cfg.xml”的一部分。 而且在编辑测试运行配置中,已经进行了部署,使了把hibernate.cfg.xml复制到TestResults还是不好用,如果新建个控制台可以运行程序,hibernate.cfg.xml设置了总是复制

喧闹幽静的主页 喧闹幽静 | 初学一级 | 园豆:182
提问于:2011-12-22 22:58
< >
分享
最佳答案
0

你是不是没有设置xml文件的加载路径。

http://www.cnblogs.com/jams742003/archive/2009/11/10/1599569.html

你看看这个

收获园豆:20
小小刀 | 小虾三级 |园豆:1991 | 2011-12-22 23:14

小弟运行建NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration().Configure();
            SchemaExport schemaExport = new SchemaExport(cfg);
            schemaExport.Create(true, true); 就报错了 还没到调用SessionFactory

喧闹幽静 | 园豆:182 (初学一级) | 2011-12-22 23:22

@喧闹幽静: XML文件的默认“复制到输出目录”为“不复制”,这里需要修改为“始终复制”。否则出现“failed:  NHibernate.Cfg.HibernateConfigException : An exception occurred during  configuration of persistence layer. ----> System.IO.FileNotFoundException : 未能找到文件“NHibernateSample\NHibernateSample.Data.Test\bin\Debug\hibernate.cfg.xml””异常。

小小刀 | 园豆:1991 (小虾三级) | 2011-12-22 23:24

@小小刀: 这个已经修改过了

喧闹幽静 | 园豆:182 (初学一级) | 2011-12-22 23:26
小小刀 | 园豆:1991 (小虾三级) | 2011-12-22 23:26

@喧闹幽静: 你是在哪里写的hibernate.cfg.xml的地址?

小小刀 | 园豆:1991 (小虾三级) | 2011-12-22 23:31

@小小刀: 那篇文章以前看过了还是没发现 谢谢哥

喧闹幽静 | 园豆:182 (初学一级) | 2011-12-22 23:34

@小小刀: 实体类和 映射文件的那个类库

喧闹幽静 | 园豆:182 (初学一级) | 2011-12-22 23:41

@喧闹幽静: 你应该是没有设置的hibernate.cfg.xml 的路径

XmlDocument cfgXml = new XmlDocument();    cfgXml.Load("TestEnbeddedConfig.cfg.xml");

   // this should put us at the first <property> element    XmlElement propElement = cfgXml.DocumentElement.GetElementsByTagName("property")[0] as XmlElement;

   // removing this will cause it not to validate    propElement.RemoveAttribute("name");

   const string FileNameForInvalidCfg = "hibernate.invalid.cfg.xml";       cfgXml.Save(FileNameForInvalidCfg);

   Configuration cfg = new Configuration();    try    {     cfg.Configure(FileNameForInvalidCfg);    }    catch (HibernateException)    {     // just absorb it - not what we are testing    }    finally    {     // clean up the bad file - if the Configure method cleans up after     // itself we should be able to do this without problem.  If it does     // property release the resource then this won't be able to access     // the file to delete.     File.Delete(FileNameForInvalidCfg);    }

小小刀 | 园豆:1991 (小虾三级) | 2011-12-22 23:45

@小小刀:

 Configuration cfg = new Configuration();    

cfg.Configure(this.GetType().Assembly, "NHibernate.Test.TestEnbeddedConfig.cfg.xml");

小小刀 | 园豆:1991 (小虾三级) | 2011-12-22 23:46

@小小刀: 请问下这个是在哪设置的

喧闹幽静 | 园豆:182 (初学一级) | 2011-12-22 23:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册