Fluent Nhibernate在内存中使用SQLite进行DB测试 - 语法在Select语句中调用用户函数时出错

时间:2016-09-15 05:44:23

标签: c# sqlite nhibernate fluent-nhibernate

我正在尝试使用SQLite和Nhibernate 4.0以及Fluent Nhibernate进行集成测试来获取内存数据库。

其中一个实体具有此映射: Map(x => x.ShowNameLine2).Formula("FileTaxes.funcMultiplePayersExist(Id, TIN, User_Id)");

我的问题是我在运行下面的查询时遇到SQLite语法错误(我已经删除了所有非必要的部分):

SELECT 
  batch.Id, 
  FileTaxes.funcMultiplePayersExist(p.Id, p.TIN, p.User_Id) as formula 
FROM "Batch" batch
LEFT OUTER JOIN "BatchPayer" bp ON batch.Id = bp.Batch_id
LEFT OUTER JOIN "Payer" p ON bp.Payer_id = p.Id
----> System.Data.SQLite.SQLiteException : SQLite error near "(" : syntax error

我尝试定义自定义方言并注册函数:

public class CustomSQLiteDialect : SQLiteDialect
{
    protected override void RegisterColumnTypes()
    {
        base.RegisterColumnTypes();
        ...
    }

    protected override void RegisterFunctions()
    {
        base.RegisterFunctions();
        ...
        RegisterFunction("FileTaxes.funcMultiplePayersExist", new StandardSQLFunction("FileTaxes.funcMultiplePayersExist", NHibernateUtil.Int32));
    }
}

我们已在内存配置中定义如下,但在使用内存配置和内存会话工厂运行测试时仍然出现错误:

    private static NHibernate.Cfg.Configuration _configuration;

    public static FluentConfiguration InMemoryConfiguration()
    {
        if (_inMemoryConfigurationSingleton != null)
            return _inMemoryConfigurationSingleton;

        _inMemoryConfigurationSingleton =
            Fluently.Configure()
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<FormMap>().Conventions.AddFromAssemblyOf<Conventions.LengthAttributeConvention>())
                .Database(SQLiteConfiguration.Standard.InMemory().ShowSql().Driver<CustomSQLite20Driver>().Dialect<CustomSQLiteDialect>();)
                .ExposeConfiguration(config =>
                {
                    config.BeforeBindMapping += BeforeBindMappingHandler;
                    var schemaExport = new SchemaExport(config);
                    _configuration = config;
                    schemaExport.Create(true, true);
                });
        _inMemoryConfigurationSingleton.BuildConfiguration();

        return _inMemoryConfigurationSingleton;
    }

    public static ISessionFactory InMemorySessionFactory()
    {
        return _inMemorySessionFactorySingleton ?? (_inMemorySessionFactorySingleton = InMemoryConfiguration().BuildSessionFactory());
    }

在内存配置设置期间,是否有一些我缺少的东西要注册这个功能?是否与架构导出正在运行的顺序有关?

1 个答案:

答案 0 :(得分:0)

可能在您附加事件处理程序时已经建立了映射。

我必须首先构建配置并将其用作Fluently的参数。配置以使其起作用:

var cfg = new Configuration();
cfg.BeforeBindMapping += BeforeBindMappingHandler;
var sessionFactory = Fluently.Configure(cfg)
   .... 
;