How to set default properties for variables that start with specific text?

Asked

Viewed 39 times

4

Is there any way to define the properties of a variable as default by the initials of the variables?

Something like public string obsUsuario { get; set; }, all of which start with Obs by default the property IsOptional, (or/and other properties) without having to declare each time for each variable?

modelBuilder.Entity<Usuarios>().Property(prop => prop.obsUsuario).IsOptional();  

1 answer

6


Just need to use a little Reflection

modelBuilder.Properties<string>()
        .Where(p => p.Name.StartsWith("descr"))
        .Configure(p => p.IsOptional());
  • 1

    Cool, I can even use one || here for more fields. type .Where(p => p.Name.StartsWith("descr") || p.Name.StartsWith("obs")), this will help me a lot. vlw

Browser other questions tagged

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