jenson-button-event
jenson-button-event

Reputation: 18941

NHibernate - Generate on insert preventing update of value

I've create a Fluent convention for mapping a specific attribute that conveys that a datetime property won't be null and will have the database insert today's date for its value, this is its code:

instance.Not.Nullable();
instance.Default("getdate()");
instance.Generated.Insert();

The insert works as expected. What's not expected is that when I update the record and set the property ("Modified") to today's date, the value is not persisted - i can see from the sql statement generated by NH that it is not in the statement. I tried adding to no avail.

instance.Update();

I am right in thinking this should work?

Upvotes: 3

Views: 1390

Answers (1)

Stefan Steinegger
Stefan Steinegger

Reputation: 64628

No, this doesn't need to work. If you map a property as generated, it is only generated by the database and can't be changed by the application. You specify that it is only generated when inserting, not when updating, because NH needs to fetch the generated value after insert, and doesn't need to after update.

To set default values, I would use constructors and other techniques of object oriented programming.

Upvotes: 4

Related Questions