Fluent-NHibernate – Smooth NHibernate and calculation properties

I am using Fluent NHibernate and automatically map classes.

I have a calculated attribute in a class

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

This is not mapped with other attributes, so I cannot use it in ICriteria restrictions. I added an empty setter( Because I read somewhere that doing so would include it in the mapping, and it does), but now I am getting an N​​Hibernate error:

could not execute query… Invalid column name’LastActionTimeStamp’.

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

You can associate formulas with attributes and use the formula instead of c# code. For example,< p>

Attributes:

private int _postCount = 0;
public virtual int PostCount
{
get< br /> {
return _postCount;
}
}

Formula:

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

Then you can use PostCount in the expression as usual. Remember to set the access modifier on the attribute in the FluentMapping. Not sure what Fluent supports, but the normal mapping options I found 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

It may be best to check the NHibernate document of the official list Access Strategies in order to combine the access strategy with the naming strategy, for example: “field .lowercase underscore”

I am using Fluent NHibernate and automatically map the class.

I have a calculated attribute in a class< /p>

public virtual DateTime? Las tActionTimeStamp
{
get {
return Actions.Count == 0? null: Actions.OrderByDescending(
a => a.TimeStamp).ElementAt(0).TimeStamp;< br /> }
}

This is not mapped with other properties, so I cannot use it in ICriteria restrictions. I added an empty setter (because I read somewhere to do Will include it in the mapping, it does), but now I receive an N​​Hibernate 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 of this attribute, but still return the calculated value from the attribute get?

You can associate a formula with an attribute, and use the formula instead of c# code. For example,

Attribute:< /p>

private int _postCount = 0;
public virtual int PostCount
{
get
{
return _postCount;< br /> }
}

Formula:

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

Then you can use PostCount in the expression as usual. Remember to set the access modifier on the attribute in FluentMapping. Not sure what Fluent supports, but the normal mapping I found The options 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

It may be best to check the NHibernate document of the official list Access Strategies in order to combine the access strategy with the naming strategy, for example: “field.lowercase underscore”

Leave a Comment

Your email address will not be published.