【问题标题】:SqLite in memory DB and NHibernate内存数据库中的 SqlLite 和 NHibernate
【发布时间】:2011-07-22 03:39:58
【问题描述】:

我已经 nhibernate 使用 oracle 数据库,我正在尝试在内存数据库中使用 sqlite 来测试我们的查询逻辑。我相信我已经阅读、理解并确认了网上关于这个问题的每一个答案,其中一些答案是几次 :)

我收到 SQLite 错误,没有这样的表: 我也没有在 SchemaExport 调用中看到任何 ddl 命令

以下是我认为相关的设置部分。 这段代码都来自同一个方法

cfg = new Configuration()
.SetProperty(Environment.ReleaseConnections, "on_close")
.SetProperty(Environment.Dialect, typeof(SQLiteDialect).AssemblyQualifiedName)
.SetProperty(Environment.ConnectionDriver, typeof(SQLite20Driver).AssemblyQualifiedName)
.SetProperty(Environment.ConnectionString, "Data Source=:memory:;Version=3;New=True")
.SetProperty(Environment.ProxyFactoryFactoryClass, typeof(ProxyFactoryFactory).AssemblyQualifiedName)
.SetProperty(Environment.ShowSql, "true")
.SetProperty(Environment.ConnectionProvider, typeof(NHibernate.Connection.DriverConnectionProvider).AssemblyQualifiedName);
cfg.AddAssembly("MyAssembly");
_sessionFactory = cfg.BuildSessionFactory();
_session = _sessionFactory.OpenSession();
new SchemaExport(cfg).Execute(true, true, false, _session.Connection, Console.Out); <--don't see any ddl commands here
var q = from c in _session.Query<ComponentGroup>()
where !c.IsDiscontinued
select c;
var z = q.ToList(); //<--get error here

我的 .hbm.xml :

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping assembly="myassmebly" namespace="myns" xmlns="urn:nhibernate-mapping-2.2">
<class name="ComponentGroup" table="TBL_COMPONENT_GROUP" lazy="true" >
<id name="Id" column="N_COMPONENT_GROUP_ID" type="int">
  <generator class="native" />
</id>
<property name="AssemblyFacilityId" column="N_ASSEMBLY_FACILITY_ID" type="int" not-null="true" />
<property name="Name" column="C_COMPONENT_GROUP_NAME"    type="string" not-null="true" />
<property name="IsDiscontinued" column="N_DISCONTINUED_FLAG" type="bool" not-null="true" />
</class>
</hibernate-mapping>

我的 POCO

public class ComponentGroup
{
    public virtual int Id { get; set; }
    public virtual int AssemblyFacilityId { get; set; }
    public virtual string Name { get; set; }
    public virtual bool IsDiscontinued { get; set; }
}

内部异常

  InnerException: System.Data.SQLite.SQLiteException
   Message=SQLite error
   no such table: TBL_COMPONENT_GROUP
   Source=System.Data.SQLite
   ErrorCode=-2147467259
   StackTrace:
        at System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn, String strSql, SQLiteStatement previous, UInt32 timeoutMS, 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)

我正在使用 NHibernate 版本 3.1.0.4,System.Date.SQLite 版本 2.0.50727

【问题讨论】:

  • 你能检查一下你的 hbm.xml 映射是否真的设置为嵌入式资源吗?
  • 它是,相同的 hbm 与 oracle 会话一起使用

标签: nhibernate sqlite


【解决方案1】:

所以解决方法是替换 新的 SchemaExport(cfg).Execute(true, true, false, _session.Connection, Console.Out); 和 new SchemaExport(cfg).Execute(true, true, false, _session.Connection, null);

真的不知道为什么,但我添加了代码以将行插入到导出的表中,结果按预期工作。

然后我将 Console.Out 参数放回原处并得到“无法写入已关闭的 TextWriter”。错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多