C#Fluently SQLLite InMemory System.Data.SQLite.SQLiteException:SQL逻辑错误或缺少数据库附近“(”:语法错误

时间:2017-08-11 01:37:33

标签: c# sqlite fluent-nhibernate nunit

 public void Initialize()
    {
        sessionFactory = CreateSessionFactory();
    }

    private ISessionFactory CreateSessionFactory()
    {
        return Fluently.Configure()
            .Database(SQLiteConfiguration.Standard.InMemory().ShowSql())
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<TestMetaData>())
            .ExposeConfiguration(cfg => configuration = cfg)
            .BuildSessionFactory();
    }

    public ISession OpenSession()
    {
        ISession session = sessionFactory.OpenSession();

        var export = new SchemaExport(configuration);
        export.Execute(true, true, false, session.Connection, null);

        return session;
    }

此部分产生错误 System.Data.SQLite.SQLiteException:SQL逻辑错误或缺少数据库附近“(”:语法错误

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我认为问题在于您何时在公开的配置上应用模式的导出。您应该在构建会话工厂之前执行此操作。

创建了设置它的sepparated方法 OpenSession()后,我怀疑你是否正在错误地应用它。

因此,您的会话工厂创建应该类似于

private ISessionFactory CreateSessionFactory()
{
    //the session in which you might want to export your schema
    ISession session = sessionFactory.OpenSession();

    return Fluently.Configure()
        .Database(SQLiteConfiguration.Standard.InMemory().ShowSql())
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<TestMetaData>())
        .ExposeConfiguration(cfg =>
         {
              //we set the configuration here, and execute it, 
              //before the session factory is built.
              var export = new SchemaExport(cfg);
              export.Execute(true, true, false, session.Connection, null);
         })
        .BuildSessionFactory();
}

¿你可以试一试,看看问题是否解决了?