Default Value does not work with DateTime and Fluent Nhibernate 1.2?

1,361 views
Skip to first unread message

chobo5

unread,
May 23, 2011, 3:51:16 PM5/23/11
to Fluent NHibernate
I have datetime column and I want to put a default value if none is
provided.

So in my fluent mapping I did

Map(x => x.EndOfTerm).Default("5/21/2011").Not.Nullable();

Yet everytime I try to save something without a EndOfTerm specified it
crashes

SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and
12/31/9999 11:59:59 PM.

When I give EndOfTerm a value it does not crash anymore.

James Gregory

unread,
May 24, 2011, 4:08:12 AM5/24/11
to fluent-n...@googlegroups.com
The Default method is used to specify what the default constraint should be on the column when you're generating your schema from your mappings. If you're not using SchemaExport/Update, then that method does nothing. To specify a default value for a property/field, just assign it like you would a normal field; set it in the field definition or in the constructor of your entity, nothing fancy required.

chobo5

unread,
May 24, 2011, 1:13:19 PM5/24/11
to Fluent NHibernate
What do you mean default constraint? So does this fill in the
"default" value for the column say in MS SQL 2008?

James Gregory

unread,
May 25, 2011, 4:03:08 AM5/25/11
to fluent-n...@googlegroups.com
Yes, if you generate your schema using the SchemaExport tool. If you're not, it has no effect.

Vahid

unread,
May 25, 2011, 8:11:58 AM5/25/11
to Fluent NHibernate
actually it works if you write something like:
Map(x =>
x.EndOfTerm).Default("5/21/2011").Not.Nullable().Not.Insert().Not.Update().Generated.Always();
http://nhforge.org/doc/nh/en/index.html#mapping-generated

James Gregory

unread,
May 25, 2011, 8:21:43 AM5/25/11
to fluent-n...@googlegroups.com
What does the Default do in that case, if you're not generating your schema? The Generated.Always tells NHibernate that it should use whatever the DB server generates.
Reply all
Reply to author
Forward
0 new messages