NHibernate中的公式映射到SQLite或覆盖映射中的函数 [英] Formula Mapping in NHibernate to Functions in SQLite or Override Mapping

查看:117
本文介绍了NHibernate中的公式映射到SQLite或覆盖映射中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现以下文章 http:// ayende .com / Blog / archive / 2009/04/28 / nhibernate-unit-testing.aspx 来帮助我测试我的NHibernate应用程序。我的大多数测试工作正常,但一个奇怪的错误已被抛出。从查看生成的SQL我相信以下流利的映射是造成的问题:

pre code Map(x => x.IsValid ).Formula(CASE WHEN dbo.NumPosts(UserID)= 5 THEN 1 ELSE 0 END);

您会注意到dbo.NumPosts的调用,它是我的数据库中用户定义的函数。我知道还有其他的方法可以映射这个属性,但这只是一个例子。基本上我需要知道如何使用SQLite来映射它。

编辑: >经过深思熟虑后,是否可以在我的单元测试项目中覆盖这个字段的映射?这是我目前的配置:

pre $私有静态ISessionFactory CreateSessionFactory(){
返回流利.Configure()
。数据库(SQLiteConfiguration.Standard.InMemory().ShowSql())
.Mappings(m => m.FluentMappings
.AddFromAssembly(typeof(Role).Assembly)
.Conventions。 AddFromAssemblyOf< EnumConvention>())
.ExposeConfiguration(c => _configuration = c)
.BuildSessionFactory();



$ b我不希望重新定义所有的映射,有些时候,会变得不可维护。

我很感激帮助。谢谢

解决方案

问题解决了!我可以这样说:
$ b $ pre $ $ $ $ $ $ $ $ $
public class NumPosts:SQLiteFunction {
public override object Invoke(object [] args){
...
}
}
$ b现在我所要做的就是添加一个设置来添加我的函数的dbo前缀infront。然后在测试项目中将其设置为空。


i found the following article http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx to help me unit test my NHibernate application. Most of my tests are working fine but a strange error has been thrown. From looking at the SQL generated i believe the following fluent mapping is causing the problems:

Map(x => x.IsValid).Formula("CASE WHEN dbo.NumPosts(UserID) = 5 THEN 1 ELSE 0 END");

You'll notice the call to dbo.NumPosts which is a user defined function within my database. I know there's other ways i could've mapped this property but this is just used as an example. Basically i need to know how to map this using SQLite.

Edit:

After further thoughts would, is it possible to override the mapping for this field in my unit testing project? Here's my current configuration:

private static ISessionFactory CreateSessionFactory() {
    return Fluently.Configure()
        .Database(SQLiteConfiguration.Standard.InMemory().ShowSql())
        .Mappings(m => m.FluentMappings
            .AddFromAssembly(typeof(Role).Assembly)
            .Conventions.AddFromAssemblyOf<EnumConvention>())
        .ExposeConfiguration(c => _configuration = c)
        .BuildSessionFactory();
}

I don't wish to re-define all the mappings as this would take some time and would become un-maintainable.

I'd appreciate the help. Thanks

解决方案

Problem solved! I was able to say:

[SQLiteFunction(Name = "NumPosts", Arguments = 1, FuncType = FunctionType.Scalar)]
public class NumPosts : SQLiteFunction {
    public override object Invoke(object[] args) {
        ...
    }
}

Now all i had to do was add a setting to add the dbo prefix infront of my functions. This is then set to blank in the test project.

这篇关于NHibernate中的公式映射到SQLite或覆盖映射中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆