Problems with not null fields creation with FluentNHibernate

1

I have a problem creating fields using FluentNhibernate and Postgres. User Class:

public class Usuario {
    public virtual int idUsuario { get; set; }
    public virtual string nome { get; set; }
    [NotNullNotEmpty]
    public virtual string login { get; set; }
    [NotNullNotEmpty]
    public virtual bool ativo { get; set; }
    [NotNullNotEmpty]
    public virtual string senha { get; set; }
}

UserMap:

public UsuarioMap() {
    Table("usuario");
    Id(x => x.idUsuario).GeneratedBy.Identity().Column("id_usuario");
    Map(x => x.nome).Column("nome").Length(50);
    Map(x => x.login).Column("login").Not.Nullable().Length(30);
    Map(x => x.ativo).Column("ativo").Not.Nullable();
    Map(x => x.senha).Column("senha").Not.Nullable().Length(50);
}

If the table does not exist, it normally creates all fields, with correct size and validation not null:

CREATE TABLE public.usuario
(
    id_usuario integer NOT NULL DEFAULT nextval('usuario_id_usuario_seq'::regclass),
    nome character varying(50),
    login character varying(30) NOT NULL,
    ativo boolean NOT NULL,
    senha character varying(50) NOT NULL, 
    CONSTRAINT usuario_pkey PRIMARY KEY (id_usuario)
)

But if the table exists and adds a field in the map class, it just creates the field without the validations:

**Class Usuario**
[NotNullNotEmpty]
public virtual string teste { get; set; }

.

**Class UsuarioMap**
Map(x => x.teste).Column("teste").Length(60).Not.Nullable();

.

CREATE TABLE public.usuario
(
    id_usuario integer NOT NULL DEFAULT nextval('usuario_id_usuario_seq'::regclass),
    nome character varying(50),
    login character varying(30) NOT NULL,
    ativo boolean NOT NULL,
    senha character varying(50) NOT NULL,
    teste character varying(60),
    CONSTRAINT usuario_pkey PRIMARY KEY (id_usuario)
)

If someone has already gone through this and can help me, thank you because I've been searching for two days and I could not solve it.

    
asked by anonymous 01.10.2016 / 18:52

1 answer

0

The default value for this field has not been set and a value has to be passed, because when creating a new field in an existing table if no value is passed, it must be null , by default. Soon this will put the appropriate rules and validations for this field.

Map(x => x.teste).Column("teste").Default("teste").Length(60).Not.Nullable();

Referrer: SOen - Specifiying Default Value for Datetime property in FluenNhibernate mapping class

    
02.10.2016 / 01:42