我们有一个使用NHibernate / FluentNHibernate和一个指向我们的sql环境的MssqlConfiguration.Mssql2008.ConnectionString的应用程序. sql服务器有几个数据库,我们可以使用如下的约定连接到不同的数据库:
public class FactseDatabaseConvention : IClassConvention { public void Apply(IClassInstance instance) { if (instance.EntityType.Namespace.EndsWith("Model.OtherEntities")) { instance.Schema("OtherDatabase.dbo"); } } }
这样可以生成正确的查询以访问OtherDatabase.当我们想要使用带有SessionFactory的sqliteConfiguration.Standard.InMemory()进行测试时,会出现问题. sqlite准备时,Persistence测试失败:
System.Data.sqlite.sqliteException : sql logic error or missing database unknown database OtherDatabase
这是它生成的命令:
create table OtherDatabse.dbo_my_other_entity_table ( Id UNIQUEIDENTIFIER not null,... other properties )
有没有办法可以更改我的sqliteConfiguration,让它创建2个内存数据库,如果是这样,怎么样?或者我应该创建一个单独的会话来测试这些其他实体?
解决方法
我遇到了同样的问题 – (自从我们从sqlite转移到sql Server LocalDB进行测试时).
我仍然有代码,我们所做的是提供一个组件,它配置是否使用模式,因此:
public class sqlLiteMappingsHelper : IMappingsHelper { public string TextColumnTableSpecification { get { // see sqlite faqs re: all varchars are very large whatever you specify // http://www.sqlite.org/faq.html#q9 return "nvarchar(10)"; } } public bool SchemasEnabled { get { return false; } } }
(对于具有SchemasEnabled = true的sql server,一个类似的) – 在Web应用程序中,您告诉您的IoC容器使用sql server one,并在测试应用程序中使用sqlite.然后在你的会议中:
public void Apply(IClassInstance instance) { var helper = IoC.get<IMappingsHelper>(); if (instance.EntityType.Namespace.EndsWith("Model.OtherEntities") && helper.SchemasEnabled) { instance.Schema("OtherDatabase.dbo"); } }