【发布时间】:2011-04-12 08:05:25
【问题描述】:
不确定我问的问题是否正确,所以请多多包涵!有点 NHibernate 菜鸟。
我们正在使用 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));
}
}
我们有一个 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);
}
问题是我不知道如何告诉 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();
}
这会引发以下异常:
TestCase ... failed:
Execute
NHibernate.Exceptions.GenericADOException:
could not get or update next value[SQL: ]
---> System.Data.SQLite.SQLiteException: SQLite error
no such column: TableKey
所以我的推断是它在检查持久性规范时没有运行部署脚本。
这种情况有现成的解决方案吗?我的 Google-fu 似乎在这方面抛弃了我。
【问题讨论】:
标签: unit-testing nhibernate sqlite fluent-nhibernate persistence