流利的NHibernate和计算的属性 [英] Fluent NHibernate and computed properties

查看:132
本文介绍了流利的NHibernate和计算的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在类中有一个计算过的属性,沿着

  public virtual DateTime? LastActionTimeStamp 
{
get {
return Actions.Count == 0? null:Actions.OrderByDescending(
a => a.TimeStamp).ElementAt(0).TimeStamp;






$ p这不是与其他属性映射的,所以我不能在ICriteria限制中使用它。我添加了一个空setter(因为我读的地方,这样做会包括它在映射,它所做的),但现在我得到一个NHibernate的错误:


无法执行查询...无效的列名称'LastActionTimeStamp'。

所以我的问题是:我告诉Fluent NHibernate告诉NHibernate忽略这个属性的数据库,但仍然返回从属性get的计算值吗?

解决方案

您可以将公式与该属性相关联,并使用该公式代替c#代码。例如



属性:

pre prerivate int _postCount = 0;
public virtual int PostCount
{
get
{
return _postCount;














pre $ (SELECT count(p.ID)FROM BlogPost p WHERE p.BlogID = ID and p.IsDeleted = 0)

然后像往常一样在表达式中使用PostCount。请记住在您的FluentMapping属性中设置访问修饰符。不知道Fluent支持的是什么,但是我在正常映射中找到的选项是:

$ p $ $ $ $ $ $ $ $ $ $ $ b $ $ b * field.camelcase
* field.camelcase-underscore
* field.pascalcase-m-underscore
* field.lowercase-underscore
* nosetter.camelcase
* nosetter.camelcase-undercore
* nosetter.pascalcase -m-underscore
* nosetter.lowercase-underscore

可能最好查看官方列表的NHibernate文档,以便将访问策略与命名策略结合起来field.lowercase-underscore


I'm using Fluent NHibernate, and auto-mapping the classes.

I have a computed property in a class along the lines of

public virtual DateTime? LastActionTimeStamp
{
    get {
        return Actions.Count == 0 ? null : Actions.OrderByDescending(
            a => a.TimeStamp).ElementAt(0).TimeStamp;
    }
}

This wasn't mapped with the rest of the properties, so I couldn't use it in an ICriteria restriction. I added an empty setter (as I read somewhere that doing so would include it in the mapping, which it does), but now I'm getting an NHibernate error:

could not execute query ... Invalid column name 'LastActionTimeStamp'.

So my question is: how do I tell Fluent NHibernate to tell NHibernate to ignore the database for this property, but still return the calculated value from the property get?

解决方案

You could associate a formula with the property and use that instead of the c# code. e.g.

Property:

private int _postCount = 0;
public virtual int PostCount
{
    get
    {
        return _postCount;
    }
}

Formula:

(SELECT count(p.ID) FROM BlogPost p WHERE p.BlogID = ID and p.IsDeleted = 0)

And then you can use PostCount in your expression as usual. Remember to set the access modifier on the property in your FluentMapping. Not sure what Fluent supports but the options I have found for normal mapping are:

* property
* field
* field.camelcase
* field.camelcase-underscore
* field.pascalcase-m-underscore
* field.lowercase-underscore
* nosetter.camelcase
* nosetter.camelcase-underscore
* nosetter.pascalcase-m-underscore
* nosetter.lowercase-underscore

Probably best check out NHibernate documentation for the official list Access Strategies so you combine the access strategy with the naming strategy e.g. "field.lowercase-underscore"

这篇关于流利的NHibernate和计算的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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