C# - Nhibernate的问题

时间:2010-12-24 16:26:43

标签: c# nhibernate fluent-nhibernate automapping

我对以下测试用例感到特别困惑:

public void TestMapping()
{
    var autoPersistenceModel = AutoMap.AssemblyOf<RepositoryEntity>().Where(
        x => x.Namespace.EndsWith("Descriptors"));

    var configuration = Fluently.Configure()
        .Database(SQLiteConfiguration.Standard.ShowSql().InMemory)
        .Mappings(x => x.AutoMappings.Add(autoPersistenceModel))
        .ExposeConfiguration(x => new NHibernate.Tool.hbm2ddl.SchemaExport(x).Create(true, false));

    var sessionFactory = configuration.BuildSessionFactory();

    using (var session = sessionFactory.OpenSession())
    {
        new PersistenceSpecification<IndicatorUnitDescriptor>(session)
            .CheckProperty(x => x.Name, "Name1")
            .CheckProperty(x => x.Timestamp, new DateTime(2000, 10, 10))
            .VerifyTheMappings();
    }
}

正如您所看到的,我正在尝试使用自动化,但不幸的是,以下测试用例引发了以下SQLite异常(第一个包含完成的实际查询):

drop table if exists "IndicatorUnitDescriptor"

drop table if exists "StockUnitDescriptor"

create table "IndicatorUnitDescriptor" (
   Id  integer,
   Name TEXT,
   Timestamp DATETIME,
   primary key (Id)
)

create table "StockUnitDescriptor" (
   Id  integer,
   Name TEXT,
   Timestamp DATETIME,
   primary key (Id)
)

NHibernate: INSERT INTO "IndicatorUnitDescriptor" (Name, Timestamp) VALUES (@p0, @p1); select last_insert_rowid();@p0 = 'Name1' [Type: String (0)], @p1 = 10.10.2000 0:00:00 [Type: DateTime (0)]

System.Data.SQLite.SQLiteException: SQLite error
no such table: IndicatorUnitDescriptor

我无法理解为什么会这样发生 - SQL命令似乎正常工作,相应的表应该由create table查询创建。

我认为我的代码出了问题(我可能错过了一些东西)。你能帮帮我吗?

1 个答案:

答案 0 :(得分:1)

我认为你使用两个会话。一个在数据库创建期间和您的测试中。尝试这样设置。

  Configuration cfg = Fluently.Configure()
        .Database(SQLiteConfiguration.Standard.InMemory())
        .Mappings(m => m.HbmMappings.AddFromAssembly(_mappingsAssembly))
        .BuildConfiguration();

    var session = cfg.BuildSessionFactory().OpenSession();

    new SchemaExport(cfg).Execute(false, true, false, session.Connection, null);