Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 884989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:50:08+00:00 2026-05-15T12:50:08+00:00

Does anyone know of a quick way to have NHibernate insert default values for

  • 0

Does anyone know of a quick way to have NHibernate insert default values for value types as null into the database?

For example:

public class Foo
{
    public int SomeProperty { get; set; }
}

By default, int is 0. When it goes into the database, I would like it to go in as null.
I know I could do this by changing SomeProperty to a nullable int, but perhaps there is another way?
I also know that I could do this with an IUserType but is there an easy way of building a user type that can be generically used for all value types?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:50:09+00:00Added an answer on May 15, 2026 at 12:50 pm

    This is not a simple way to make NHibe convert 0 to null and vice-versa, but an alternative suggestion (maybe or maybe not right in your situation).

    Imo, using a default value to mean null is an antipattern; maybe you agree. I take it that you’re trying to hook up some legacy code with a proper database; is that correct?

    If you are in control of the business class which you are mapping (class Foo), then I recommend exposing a nullable version of SomeProperty for use moving forward:

    public class Foo
    {
        /// for new code, and for mapping to the database:
        public int? SomeProperty_new {get;set;}
    
        /// for legacy code only.  Eventually, refactor legacy code to use SomeProperty_new instead, and just remove this needless property.
        [Obsolete("This uses default value to mean null.  Use SomeProperty_new instead.")]
        public int SomeProperty_old
        { 
            get 
            {
                if (SomeProperty_new == null)
                    return 0;
                else
                    return SomeProperty_new;
            }
            set { /* convert from 0 to null if necessary and set to SomeProperty_new.*/ }
        }
    }
    

    You’ll want better names than SomeProperty_new and SomeProperty_old, though.

    On the other hand, if 0 is never a valid value (except to mean null), you could instead make the DB use a non-nullable value. It really depends on the situation at hand.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know of a quick way to disable the Enterprise Library integration with
Does anyone know a quick and dirty threadsafe vector class for c++? I am
I am using dasBlog for my Blog and one of the categories I have
I have a DotNetNuke site where my main logo is a PNG file using
Ok, I have tried searching around for this answer, but with no luck. I
Background: I am using ASP.NET 2.0 (with C#) and the code below is embedded
Here, we run a set of Visual Studio 2008's CA rules both locally and
An annoyance that I sometimes come across with SVN is the working copy getting
I'm using R under Windows XP. It picked up the environmental variable HOME from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.