NHibernate Validator and Schema Export question

468 Views Asked by At

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).

3

There are 3 best solutions below

0
On BEST ANSWER

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,
   ...
)
0
On

NHibernate Validator sits on top of NHibernate. It is used to validate entities against NHibernate mappings and custom rules. For configuring field properties, such as whether they are nullable, this is done in the NHibernate mappings as it affects not only the validations done, but also the generated SQL statements.

0
On

You gave the answer already. The validator is not scanned by schema export. You have to use the mapping.