好吧,这是我的非常非常流畅的hibernate项目。我在hibernate和nhibernate中有很少的经验。如何用流利的nhibernate(schemaexport)测试生成表?在asp.net上下文
由于这是一个网络应用程序项目,所以这个上下文对我来说是全新的。 所以我有我的webapp项目与大多数在网上找到的流利nhibernate。 所以我有这样的实体:
namespace myproject.model
{
public class Request
{
public virtual string Id { get; private set; }
public virtual Route route { get; set; }
public virtual int code { get; set; }
}
}
namespace myproject.model
{
public class Route
{
public virtual string Id { get; private set; }
public virtual string client_id { get; set; }
public virtual IList<Request> requests { get; set; }
public Route()
{
requests = new List<Request>();
}
}
}
//Mapping are like this.will only post one
namespace myproject.mappings
{
public class RequestMap : ClassMap<Request>
{
public RequestMap()
{
Id(x => x.Id);
Map(x => x.short_code);
References(x => x.route);
}
}
}
//NhibernateSessionPerRequest
namespace myproject.Boostrap
{
public class NhibernateSessionPerRequest : IHttpModule
{
private static readonly ISessionFactory _sessionFactory;
static NhibernateSessionPerRequest()
{
_sessionFactory = CreateSessionFactory();
}
//all others IHttpModule event and methods are here
private static ISessionFactory CreateSessionFactory()
{
FluentConfiguration configuration = Fluently.Configure().Database(MsSqlConfiguration.MsSql2005.
ConnectionString(x => x.FromConnectionStringWithKey("localdb")))
.Mappings(m => {
m.FluentMappings.AddFromAssemblyOf<myproject.model.Request>();
m.FluentMappings.AddFromAssemblyOf<myproject.model.Route>();
}
).ExposeConfiguration((c)=> savedConfig = c);;
return configuration.BuildSessionFactory();
}
}
private static Configuration savedConfig;
public static void BuildSchema(NHibernate.Cfg.Configuration config)
{
new SchemaExport(config).Create(false, true);
}
public static void BuildSchema(ISession session)
{
var export = new SchemaExport(savedConfig);
export.Execute(false,true,false,session.Connection,null);
}
}
我为了测试我添加了一个测试项目(类库)表的生成添加模块webconfig
<add name="NhibernateSessionPerRequest" type="myproject.Boostrap.NhibernateSessionPerRequest"/>
添加参考nunit.framework 2.8.5和myproject。
namespace myproject.Tests
{
[TestFixture]
public class CanGenerateSchemaTestSuite
{
[Test]
public void CanGenarateSchema()
{
NhibernateSessionPerRequest.BuildSchema(NhibernateSessionPerRequest.GetCurrentSession());
}
}
}
的测试方法总是失败,我具有此异常:
CanGenerateSchemaTestSuite(1个试验),1次测试失败:儿童测试失败 CanGenarateSchema,失败:System.TypeInitializationException
那么在asp.net环境下如何测试? 感谢您阅读本文。谢谢
哦,那真的很有你。我已经看到了这个窍门。但还有一个问题仍然存在。对于if.I '必须在我的'NhibernateSessionPerRequest'类和'BuildSchem'类中创建private.So如何运行'BuildSchema'方法,甚至进一步如何在我的'测试项目'中运行它?谢谢 – 2010-11-17 12:34:19
正如你所看到的,我有自己的ISessionFactoryProvider,它被注入会话工厂单例中。对于单元测试,我使用一个测试来完成构建。这是你的意思吗? – Aliostad 2010-11-17 13:04:31
你好,我没有使用任何类型的依赖注入图书馆,我的问题在这里不能够弄清楚如何在一个单独的项目(意思是我的测试项目)测试这个,我已经更新了这篇文章的标题,并添加新的方法,我从你的答案中得到。请让我知道如果我错了。感谢您的时间 – 2010-11-17 14:03:14