流畅的NHibernate - HiLo方案的持久性规范

Mar*_*Pim 5 sqlite nhibernate persistence unit-testing fluent-nhibernate

不确定我是否在问正确的问题所以请耐心等待!一点NHibernate noob.

我们正在使用Fluent NH并为所有表格提供以下id生成方案

public class IdGenerationConvention : IIdConvention
{
    public void Apply(IIdentityInstance instance)
    {
        var where = string.Format("TableKey = '{0}'", instance.EntityType.Name);
        instance.GeneratedBy.HiLo("HiloPrimaryKeys", "NextHighValue", "1000", x => x.AddParam("where", where));
    }
}
Run Code Online (Sandbox Code Playgroud)

我们有一个SQL脚本,可以生成HiloPrimaryKeys表并使用在部署期间运行的数据对其进行播种.这工作正常.

我现在正在尝试编写单元测试以验证我们的持久层,理想情况下在内存配置中使用SQLite来提高速度.这是我为测试配置NH的方式:

[SetUp]
public void SetupContext()
{
    config = new SQLiteConfiguration()
            .InMemory()
            .ShowSql()
            .Raw("hibernate.generate_statistics", "true");

    var nhConfig = Fluently.Configure()
            .Database(PersistenceConfigurer)
            .Mappings(mappings =>
                 mappings.FluentMappings.AddFromAssemblyOf<DocumentMap>()
            .Conventions.AddFromAssemblyOf<IdGenerationConvention>());

    SessionSource = new SessionSource(nhConfig);
    Session = SessionSource.CreateSession();
    SessionSource.BuildSchema(Session);
}
Run Code Online (Sandbox Code Playgroud)

问题是我不知道如何告诉NHibernate我们的部署脚本,以便它在测试期间生成正确的模式和种子数据.

我得到的具体问题是运行以下PersistenceSpecification测试时:

[Test]
public void ShouldAddDocumentToDatabaseWithSimpleValues()
{
    new PersistenceSpecification<Document>(Session)
            .CheckProperty(x => x.CreatedBy, "anonymous")
            .CheckProperty(x => x.CreatedOn, new DateTime(1954, 12, 23))
            .CheckProperty(x => x.Reference, "anonymous")
            .CheckProperty(x => x.IsMigrated, true)
            .CheckReference(x => x.DocumentType, documentType)
            .VerifyTheMappings();
}
Run Code Online (Sandbox Code Playgroud)

这会引发以下异常:

TestCase ... failed: 
Execute
NHibernate.Exceptions.GenericADOException: 
        could not get or update next value[SQL: ] 
        ---> System.Data.SQLite.SQLiteException: SQLite error
        no such column: TableKey
Run Code Online (Sandbox Code Playgroud)

所以我的推论是,它在检查持久性规范时没有运行部署脚本.

这种情况是否存在解决方案?我的Google-fu似乎已经抛弃了我.

Jak*_*art 4

正如 Brian 所说,您可以在构建架构后运行部署脚本。这段代码对我来说效果很好:

var config = new SQLiteConfiguration()
        .InMemory()
        .ShowSql()
        .Raw("hibernate.generate_statistics", "true");

var nhConfig = Fluently.Configure()
        .Database(config)
        .Mappings(mappings =>
             mappings.FluentMappings.AddFromAssemblyOf<DocumentMap>()
        .Conventions.AddFromAssemblyOf<IdGenerationConvention>());

var SessionSource = new SessionSource(nhConfig);
var Session = SessionSource.CreateSession();
SessionSource.BuildSchema(Session);

// run the deployment script
var deploymentScriptQuery = Session.CreateSQLQuery("ALTER TABLE HiloPrimaryKeys ADD COLUMN TableKey VARCHAR(255); INSERT INTO HiloPrimaryKeys (TableKey, NextHighValue) values ('Document', 1);");
deploymentScriptQuery.ExecuteUpdate();
Run Code Online (Sandbox Code Playgroud)

部署脚本可以从文件等加载......

构建 FNH 配置和数据库架构是一项耗时的操作。如果使用模式的测试数量增加并且模式和配置是由每个测试类构建的,则测试套件的执行将花费不可接受的时间。配置和模式都应该在所有测试之间共享。以下是如何在不失去测试隔离的情况下实现这一目标。

编辑:如果测试中需要多个会话实例,则应打开连接池,或者应通过同一连接创建两个会话。详细信息在这里...