Search code examples
nhibernatenhibernate-mappingnhibernate-mapping-by-code

How do I specify a property as required (NOT NULLABLE)?


I am trying my hand at NHibernate's built-in mapping by code. I've got it mostly working now. My problem is how do I configure which properties on my objects are required in the database within the convention? I'm guessing this would be some sort of attribute markup?

I know if I do the mappings by hand, I can configure them as required, but how do I do this with conventions?


Solution

  • Simply use nullable types, for example in the class,

    public class Foo
    {
       public int Bar { get; set; }
       public int? baz { get; set; }
    }
    

    Bar will not be nullable, while Baz will be nullable in the db when use mapping by code.