I have a domain class that looks like this. I want NHibernate to save the current value of LastUpdate
when inserting/updating so that I can use it in queries, but to ignore it when retrieving a Foo
from the database and let the object itself recalculate the value when I actually access it.
public class Foo {
public DateTime LastUpdate {
get {
/* Complex logic to determine last update by inspecting History */
return value;
}
}
public IEnumerable<History> History { get; set; }
/* etc. */
}
My mapping for Foo
looks like this:
public class FooMap : ClassMap<Foo> {
Map(x => x.LastUpdate)
.ReadOnly();
HasMany(x => x.History);
// etc...
}
I thought that ReadOnly()
was what I wanted to accomplish this, but when I try to create a SessionFactory I get the following exception:
Error: FluentNHibernate.Cfg.FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.
---> NHibernate.PropertyNotFoundException: Could not find a setter for property 'LastUpdate' in class 'Foo'.
The property doesn't have a setter because it shouldn't be set, only read from. Is ReadOnly()
the correct thing to do here? If not, what?
(NHibernate v3.0b1, Fluent NHibernate v1.1)
Fluent NHibernate offers an alternative to NHibernate's standard XML mapping files. Rather than writing XML documents, you write mappings in strongly typed C# code. This allows for easy refactoring, improved readability and more concise code.
NHibernate is a port of Hibernate from Java, one of the oldest and most respected Object-Relational Mappers (ORMs).
NHibernate is a mature, open source object-relational mapper for the . NET framework. It's actively developed, fully featured and used in thousands of successful projects. Easily map regular C# or VB.NET object models designed in Visual Studio.
ReadOnly
instructs Fluent NHibernate to not look for changes on this property, this does not equate to a read-only property in compiler-world. Your property isn't read-only in NHibernate's eyes because you're expecting it to be populated from your database. What you need to do is tell NHibernate that it should access the value of that property through a private field with the same name (lowercased) as the property.
Map(x => x.LastUpdate)
.Access.Field();
There are several alternatives to using Field
, which one you use will depend on how you name your private fields.
As far as NHibernate goes, you can map to a field, i.e member variable so Nhibernate can access the member variable directly. So you can create a member variable like _lastUpdate that can be mapped directly. Nhibernate will now have a variable to use and you can control the value separately in your getter because NHibernate will no longer use the property getter. It will save the value, and retrieve it too, but the retrieved value should not matter because as soon as you access through your getter you can recalculate it. Ditto for private variables with no getters or setters.
In a regular hbm you would just map access=field. Everyone does it. Apparently Fluent is not a simple. I don't use Fluent ...
EDIT ...
find whatever the seemingly always moving target for mapping private backing fields is in your version and use that ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With