流畅的 NHibernate 与 SQLite

发布于 2024-10-07 16:36:37 字数 3682 浏览 0 评论 0原文

我正在使用 SQLite 进行单元测试。

private ISession _session;
    [SetUp]
    public virtual void SetUp()
    {
        string[] mappingAssemblies = RepositoryTestsHelper.GetMappingAssemblies();
        configuration = NHibernateSession.Init(new SimpleSessionStorage(), mappingAssemblies,
                               new AutoPersistenceModelGenerator().Generate(),
                               @"C:\Sharp Architecture\SharpArchitecture_1_6_FullSourceAndTemplates (1)\src\NorthwindSample\tests\Northwind.Tests\Hibernate.cfg.xml");
        _session = NHibernateSession.GetDefaultSessionFactory().OpenSession();
        new SchemaExport(configuration).Execute(false, true, false, _session.Connection, null); 
    }

    [TearDown]
    public virtual void TearDown()
    {
        NHibernateSession.CloseAllSessions();
        NHibernateSession.Reset();
    }

    [Test]
    public void CanConfirmDatabaseMatchesMappings()
    {
        var allClassMetadata = NHibernateSession.GetDefaultSessionFactory().GetAllClassMetadata();
        foreach (var entry in allClassMetadata)
        {

            NHibernateSession.Current.CreateCriteria(entry.Value.GetMappedClass(EntityMode.Poco))
                 .SetMaxResults(0).List();
        }
    }

我收到以下异常。

NHibernate.ADOException : could not execute query
[ SELECT this_.CategoryID as CategoryID0_0_, this_.CategoryName as Category2_0_0_ FROM Categories this_ limit 0 ]
[SQL: SELECT this_.CategoryID as CategoryID0_0_, this_.CategoryName as Category2_0_0_ FROM Categories this_ limit 0]
  ----> System.Data.SQLite.SQLiteException : SQLite error
no such table: Categories
at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters)
at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters)
at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces, IType[] resultTypes)
at NHibernate.Loader.Criteria.CriteriaLoader.List(ISessionImplementor session)
at NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results)
at NHibernate.Impl.CriteriaImpl.List(IList results)
at NHibernate.Impl.CriteriaImpl.List()
at Tests.Northwind.Data.NHibernateMaps.MappingIntegrationTests.CanConfirmDatabaseMatchesMappings() in MappingIntegrationTests.cs: line 50
--SQLiteException
at System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn, String strSql, SQLiteStatement previous, UInt32 timeoutMS, ref String strRemain)
at System.Data.SQLite.SQLiteCommand.BuildNextCommand()
at System.Data.SQLite.SQLiteCommand.GetStatement(Int32 index)
at System.Data.SQLite.SQLiteDataReader.NextResult()
at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)
at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.SQLite.SQLiteCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader()
at NHibernate.AdoNet.AbstractBatcher.ExecuteReader(IDbCommand cmd)
at NHibernate.Loader.Loader.GetResultSet(IDbCommand st, Boolean autoDiscoverTypes, Boolean callable, RowSelection selection, ISessionImplementor session)
at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) 

我正在使用夏普架构。 谢谢, 纳宾

I am using SQLite for unit test.

private ISession _session;
    [SetUp]
    public virtual void SetUp()
    {
        string[] mappingAssemblies = RepositoryTestsHelper.GetMappingAssemblies();
        configuration = NHibernateSession.Init(new SimpleSessionStorage(), mappingAssemblies,
                               new AutoPersistenceModelGenerator().Generate(),
                               @"C:\Sharp Architecture\SharpArchitecture_1_6_FullSourceAndTemplates (1)\src\NorthwindSample\tests\Northwind.Tests\Hibernate.cfg.xml");
        _session = NHibernateSession.GetDefaultSessionFactory().OpenSession();
        new SchemaExport(configuration).Execute(false, true, false, _session.Connection, null); 
    }

    [TearDown]
    public virtual void TearDown()
    {
        NHibernateSession.CloseAllSessions();
        NHibernateSession.Reset();
    }

    [Test]
    public void CanConfirmDatabaseMatchesMappings()
    {
        var allClassMetadata = NHibernateSession.GetDefaultSessionFactory().GetAllClassMetadata();
        foreach (var entry in allClassMetadata)
        {

            NHibernateSession.Current.CreateCriteria(entry.Value.GetMappedClass(EntityMode.Poco))
                 .SetMaxResults(0).List();
        }
    }

I am getting the following exception.

NHibernate.ADOException : could not execute query
[ SELECT this_.CategoryID as CategoryID0_0_, this_.CategoryName as Category2_0_0_ FROM Categories this_ limit 0 ]
[SQL: SELECT this_.CategoryID as CategoryID0_0_, this_.CategoryName as Category2_0_0_ FROM Categories this_ limit 0]
  ----> System.Data.SQLite.SQLiteException : SQLite error
no such table: Categories
at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters)
at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters)
at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces, IType[] resultTypes)
at NHibernate.Loader.Criteria.CriteriaLoader.List(ISessionImplementor session)
at NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results)
at NHibernate.Impl.CriteriaImpl.List(IList results)
at NHibernate.Impl.CriteriaImpl.List()
at Tests.Northwind.Data.NHibernateMaps.MappingIntegrationTests.CanConfirmDatabaseMatchesMappings() in MappingIntegrationTests.cs: line 50
--SQLiteException
at System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn, String strSql, SQLiteStatement previous, UInt32 timeoutMS, ref String strRemain)
at System.Data.SQLite.SQLiteCommand.BuildNextCommand()
at System.Data.SQLite.SQLiteCommand.GetStatement(Int32 index)
at System.Data.SQLite.SQLiteDataReader.NextResult()
at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)
at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.SQLite.SQLiteCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader()
at NHibernate.AdoNet.AbstractBatcher.ExecuteReader(IDbCommand cmd)
at NHibernate.Loader.Loader.GetResultSet(IDbCommand st, Boolean autoDiscoverTypes, Boolean callable, RowSelection selection, ISessionImplementor session)
at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) 

I am using Sharp Architecture.
Thanks,
Nabin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩套路吗 2024-10-14 16:36:37

看一下这个类:

FluentNHibernate.Testing.SingleConnectionSessionSourceForSQLiteInMemoryTesting

它有一个 BuildSchema() 方法。

Take a look at the class:

FluentNHibernate.Testing.SingleConnectionSessionSourceForSQLiteInMemoryTesting

It has a BuildSchema() method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文