使用SQLite In Memory配置测试NHibernate时,如何创建另一个数据库?

时间:2013-09-06 15:42:37

标签: sqlite nhibernate fluent-nhibernate nhibernate-configuration

我们有一个使用NHibernate / FluentNHibernate的应用程序,其MsSqlConfiguration.MsSql2008.ConnectionString指向我们的SQL环境。 SQL服务器有几个数据库,我们可以使用如下的约定连接到不同的数据库:

public class FactseDatabaseConvention : IClassConvention
{
    public void Apply(IClassInstance instance)
    {
        if (instance.EntityType.Namespace.EndsWith("Model.OtherEntities"))
        {
            instance.Schema("OtherDatabase.dbo");
        }
    }
}

这样可以生成正确的查询以访问OtherDatabase。当我们想要使用SQLiteConfiguration.Standard.InMemory()和SessionFactory进行测试时会出现问题。 SQLite准备时,持久性测试失败:

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个内存数据库,如果是这样,怎么办?或者我应该创建一个单独的会话来测试这些其他实体吗?

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题 - (自从我们从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");
    }
}