Nhibernate内存数据库安装程序错误

时间:2013-10-10 11:57:03

标签: .net sqlite nhibernate fluent-nhibernate

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

我接受了这个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模式有问题,当我映射没有Schema的表时它可以工作....任何关于如何使映射忽略表映射中的模式的想法:)。

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

1 个答案:

答案 0 :(得分: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 ...
    }
}