我构建了一个简单的NHibernate应用程序,但是我想使用Mapping Attributes而不是Mapping XML。
现在,当我创建一个单元测试项目并想加载映射属性时,我遇到了问题,即我的域对象在另一个项目中!
这是我的单元测试:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
namespace Tests
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
Configuration cfg = new Configuration();
cfg.Configure();
cfg.AddInputStream(NHibernate.Mapping.Attributes.HbmSerializer.Default.Serialize(
System.Reflection.Assembly.GetExecutingAssembly()));
new SchemaExport(cfg).Execute(false, true, false);
}
}
在这条线
cfg.AddInputStream(NHibernate.Mapping.Attributes.HbmSerializer.Default.Serialize(
System.Reflection.Assembly.GetExecutingAssembly()));
我得到这个异常:
Result Message:
Test method Tests.UnitTest1.TestMethod1 threw exception:
NHibernate.Mapping.Attributes.MappingException: The following assembly contains
no mapped classes: Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Result StackTrace:
at NHibernate.Mapping.Attributes.HbmSerializer.Serialize(Stream stream, Assembly assembly)
at NHibernate.Mapping.Attributes.HbmSerializer.Serialize(Assembly assembly)
at Tests.UnitTest1.TestMethod1() in ...Tests\UnitTest1.cs:Line 17.
我想代替
System.Reflection.Assembly.GetExecutingAssembly()
我必须以某种方式来组装我的主要项目。
我发现最简单的方法是:
Assembly.Load("Project Name of Desired Assembly")
我在网上阅读了很多相关内容,包括这篇文章,没有什么比使用这种方法更简单的了。另外,JoelC提到的方法可能有效,但是我无法让它们为我工作,因此没有任何问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句