How to simplify declaration of types with Entity Frameowrk?

Asked

Viewed 132 times

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?

  • @jbueno, I don’t know, how would that be? has some example?

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

  • @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.

  • About that question yesterday? There must be another problem, right.

  • @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.

Show 1 more comment

1 answer

2


The way to leave a configuration global to the Entity Framework is in the method OnModelCreating with the line

modelBuilder.Properties<DateTime>().Configure(x => x.HasColumnType("datetime"));

Follows the code:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     modelBuilder.Properties<DateTime>().Configure(x => x.HasColumnType("datetime"));
}

Observing: can be done also for other types.

Another example of configuration

Create a class and inherit with Convention and in your Construtor writes the same column configuration code DateTime.

public class DateTimeConvention : Convention
{
    public DateTimeConvention()
    {
        this.Properties<DateTime>()
            .Configure(c => c.HasColumnType("datetime"));        
    }
}

In the OnModelCreating work with convention as follows:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{   
    modelBuilder.Conventions.Add(new DateTimeConvention());
}

Some additional examples:

Setting the column size:

modelBuilder.Properties<string>()
                .Configure(c => c.HasMaxLength(500));

Setting the column size for a particular column name:

modelBuilder.Properties<string>()
                .Where(x => x.Name == "Name")
                .Configure(c => c.HasMaxLength(250));

References:

  • 1

    This get global for any right class?

  • This @Marconciliosouza, works very well this inclusive and does not need to configure in the others. I already used. The links also have citation reaffirming all this.

  • 1

    vlw, that’s just what I needed.

Browser other questions tagged

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