3
I have some tables with several fields of type datetime, and for all of them I need to create a validation in Onmodelcreating() for this type, IE, I need to define .HasColumnType("datetime");
, my doubt is;
There is a more practical way to set this somehow as a default?
Something like;
modelBuilder.Entity<Usuarios>()
.Property(so => so.Contains("dt)) // contem dt inicia dos campos datetime
.HasColumnType("datetime");
The idea and not have to repeat that bunch of times as I had to do below.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Usuarios>()
.Property(so => so.dtAdmissao)
.HasColumnName("dtAdmissao")
.HasColumnType("datetime");
modelBuilder.Entity<Usuarios>()
.Property(so => so.dtInclusao)
.HasColumnName("dtInclusao")
.HasColumnType("datetime");
modelBuilder.Entity<Usuarios>()
.Property(so => so.dtNascimento)
.HasColumnName("dtNascimento")
.HasColumnType("datetime");
base.OnModelCreating(modelBuilder);
}
It is no longer easy to stop using Fluent as a rule and start using only when it is very necessary?
– Jéf Bueno
@jbueno, I don’t know, how would that be? has some example?
– Marco Souza
Just don’t do that Mapping. EF maps automatically. Even more you are creating columns with the same name as the properties. Do this Mapping ends up being just a waste of time.
– Jéf Bueno
@jbueno, yes, but I tried to do it without using . Hascolumntype("datetime"); and I had a problem with the database at the time of doing the Insert.
– Marco Souza
About that question yesterday? There must be another problem, right.
– Jéf Bueno
@jbueno, Regarding this question yes... but yesterday’s time to save if I did not declare the above form was error in Insert. only it got boring having to do it for all the classes of the model.
– Marco Souza