1

我在设置 SQLite inMemory 数据库时遇到问题,我已经从这里http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki安装了 sqlite

我收到了这个 Nhibernate 异常

SQL逻辑错误或缺少数据库,未知数据库核心

尝试运行单元测试时,在 BuildSchema 方法中引发异常

这是我的工厂代码

Imports NHibernate.Tool.hbm2ddl

Namespace SessionManager
    Public Class Factory
        Private Sub New()
        End Sub

    Private Shared _configuration As NHibernate.Cfg.Configuration
    Private Shared _sessionFactory As NHibernate.ISessionFactory

    Public Shared Function OpenTestSession() As NHibernate.ISession
        If SessionFactory Is Nothing Then
            SessionFactory = CreateSessionFactory()
            Dim sx = SessionFactory.OpenSession()
            'BuildSchema2(sx)
            Return sx
        End If
    End Function

    Private Shared Function CreateSessionFactory() As NHibernate.ISessionFactory
        Return FluentNHibernate.Cfg.Fluently.Configure().Database(GetInMemoryDataBaseConfiguration()).Mappings(Sub(m) m.FluentMappings.AddFromAssemblyOf(Of ModelEntity)()).ExposeConfiguration(Sub(Cfg) BuildSchema3(Cfg)).BuildSessionFactory()
    End Function

    Private Shared Function CreateSessionFactory2() As NHibernate.ISessionFactory
        Return FluentNHibernate.Cfg.Fluently.Configure().Database(GetInMemoryDataBaseConfiguration()).Mappings(Sub(m) m.FluentMappings.AddFromAssemblyOf(Of ModelEntity)()).ExposeConfiguration(Function(Cfg) InlineAssignHelper(_configuration, Cfg)).BuildSessionFactory()
    End Function

    Private Shared Sub BuildSchema(config As NHibernate.Cfg.Configuration)
        Dim se = New SchemaExport(config)
        se.Create(False, True)
    End Sub

    Private Shared Sub BuildSchema2(sx As NHibernate.ISession)
        Dim se = New SchemaExport(_configuration)
        se.Execute(True, True, False, sx.Connection, Nothing)
    End Sub

    Private Shared Sub BuildSchema3(config As NHibernate.Cfg.Configuration)
        Dim se = New SchemaExport(config)
        se.Create(True, True)
    End Sub

    Private Shared Function GetInMemoryDataBaseConfiguration() As FluentNHibernate.Cfg.Db.SQLiteConfiguration
        Return FluentNHibernate.Cfg.Db.SQLiteConfiguration.Standard.InMemory()
    End Function

    Private Shared Function InlineAssignHelper(Of T)(target As T, value As T) As T
        target = value
        Return value
    End Function

End Class

结束命名空间

如果有人知道解决方案或指出我的解决方案,我将赠送积分:)

编辑

SQLite 似乎与我们的 SQL Server 架构存在问题,当我映射没有架构的表时,它可以工作......关于如何获取映射以忽略表映射中的架构的任何想法:)。

除此之外,我还发现了与此相关的问题... Fluent NHibernate - Remove Schema from Mappings for Testing with SQLite

4

1 回答 1

0

您似乎走在正确的轨道上-尽管我认为您无法让它忽略映射中的模式,除非您实现自己的方言或类似的东西。相反,由于您使用 Fluent NHibernate 来生成映射,因此请让它生成没有模式的映射。毕竟,您的 ClassMaps、AutoMappingOverrides 和 Conventions 都只是代码。一种方法是这样的:

创建一个地方来存储可以在运行时更改的设置:

public static class MappingOptions
{
    public static readonly ThreadLocal<bool> ForSQLite = new ThreadLocal<bool>();
}

在要求 FluentNHibernate 生成映射之前设置它,然后在需要的地方检查它:

public class EmployeeMap : ClassMap<Employee>
{
    public EmployeeMap()
    {
        if (!MappingOptions.ForSQLite.Value)
            Schema("core");
        Table("Employee");
        // ... other stuff ...
    }
}
于 2013-10-10T16:00:33.320 回答