如何使用流畅的nhibernate(schemaexport)测试生成表?在asp.net上下文中
这是我的第一个流畅的hibernate项目。我在hibernate和nhibernate方面经验很少。
这个上下文对我来说是全新的,因为这是一个Web应用程序项目。
所以我有我的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"/>
为了测试表的生成,我添加了一个测试项目(类库),将ref添加到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上下文中进行测试?
谢谢你读这篇文章。谢谢
没有找到相关结果
已邀请:
2 个回复
烫珊
它只是意味着您可以在不同的数据库上运行测试而无需更改其他任何内容。 编辑; woops;认为这是一个公认的解决方案。如果你在测试中这样做,就这样做:
河饶办斜施
并传递一个接受配置的方法,您只需在那里构建数据库然后使用
: