Problems with creating not null fields with Fluentnhibernate

Asked

Viewed 118 times

1

I have a problem creating the 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; }
}

User:

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 I add 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 anyone has been through this and can help me, I appreciate it because I am already two days researching and I could not solve.

1 answer

0


  • 1

    Oops, basic logic failure creating a notnull field with null value won’t even.[

  • I don’t understand?...

  • I just can’t put characters as defaut, just numbers like this: Map(x => x.test). Column("test"). Length(10). Default("12345").Not.Nullable(); Thanks in advance for the help, I think I would take a long time to figure out this error

  • @Elieltonbitencourt puts any kind of character that your statement is strange, because I did the test with the word "test" and it worked first! strange

Browser other questions tagged

You are not signed in. Login or sign up in order to post.