Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I map a property with no setter and no backing property fluently with NHibernate?

Let's say I have the following entity:

public class CalculationInfo
{
    public virtual Int64 Id { get; set; }

    public virtual decimal Amount { get; set; }

    public virtual decimal SomeVariable { get; set; }

    public virtual decimal SomeOtherVariable { get; set; }

    public virtual decimal CalculatedAmount
    { 
        get
        {
            decimal result;

            // do crazy stuff with Amount, SomeVariable and SomeOtherVariable

            return result;
        }
    }
}

Basically I want to read and write all of the fields to my database with NHibernate with the exception of CalculatedAmount, which I simply want to write and not read back in.

Every similar issue and corresponding answer has dealt with specifying a backing store for the value, which I won't have in this scenario.

How can I accomplish this using Fluent NHibernate?

Thanks!

UPDATE: Here's what I've tried, and the error it leads to:

Here's my mapping for the property...

Map(x => x.CalculatedAmount)
      .ReadOnly();

And the exception it yields...

Could not find a setter for property 'CalculatedAmount' in class 'xxx.CalculationInfo'

like image 872
Brandon Linton Avatar asked Jun 22 '10 17:06

Brandon Linton


2 Answers

I figured out that the way get this mapping working in Fluent NHibernate is to simply add the Access Property:

Map(x => x.CalculatedAmount).Access.ReadOnly();
like image 74
Ferry de Boer Avatar answered Oct 18 '22 07:10

Ferry de Boer


I don't use Fluent, but in the mapping a persisted property with no setter is mapped with access="readonly", so look for something like .Readonly()

(Readonly is from the model perspective; the value is written to the DB and used in dirty checks)

like image 21
Diego Mijelshon Avatar answered Oct 18 '22 06:10

Diego Mijelshon