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

Was it helpful?

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

OTHER TIPS

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top