【问题标题】:Fluent Nhibernate In Memory DB testing with SQLite - Syntax Error calling user function in Select statement使用 SQLite 进行 Fluent Nhibernate In Memory DB 测试 - 在 Select 语句中调用用户函数时出现语法错误
【发布时间】:2017-01-23 01:14:23
【问题描述】:

我正在尝试使用 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());
    }

在内存配置设置期间注册此功能是否缺少什么?它与模式导出的运行顺序有关吗?

【问题讨论】:

  • 不知道这是否有帮助stackoverflow.com/questions/4389672/…
  • 我在问这个问题之前发现了那个帖子。我仍然无法弄清楚。当我在 BeforeBindMapping 处理程序中设置断点时,它永远不会被调用。 RegisterFunction 方法在我的 customDialect 类中被调用,但是当我之后检查配置时,它显示没有注册任何函数。

标签: c# sqlite nhibernate fluent-nhibernate


【解决方案1】:

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

我必须先构建配置并将其用作 Fluently.Configure 的参数才能使其工作:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-21
    相关资源
    最近更新 更多