C # - Nhibernate вопрос - PullRequest
       5

C # - Nhibernate вопрос

3 голосов
/ 24 декабря 2010

Меня особенно смущает следующий тестовый пример:

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();
    }
}

Как вы видите, я экспериментирую с autopping, но, к сожалению, следующееВ тестовом примере возникает следующее исключение 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 Ответ

1 голос
/ 24 декабря 2010

Я думаю, что вы используете две сессии.Один во время создания базы данных и в самом тесте.Попробуйте настроить так

  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);
...