Search code examples
nhibernatevalidationfluent-nhibernatenhibernate-validator

NHibernate Validator and Schema Export question


I'm learning to use NHibernate validator and it's Fluent API (Loquacious).

I have noticed is that I can't set an integer property or nullable int property (int?) to be not nullable. Well, why not?

In a database, an integer column can have null values. Even worse, when I generate DDL using SchemaExport, the integer column wont be picking up that non-nullabity (unless I express it in the Nhibernate mappings).


Solution

  • If you specify the validators using ValidatorDef<>, this is detected by the the schema export, and you'll get the appropriate SQL definitions, example:

    public class InvoiceValidationDef : ValidationDef<Invoice>
    {
       public InvoiceValidationDef()
       {
           ...
           Define(x => x.Description).NotNullable().And.MaxLength(255);
           ...
       }
    }
    

    Results in

    create table Invoices (
       ...
       Description NVARCHAR2(255) not null,
       ...
    )