Convert all strings to lowercase

Asked

Viewed 89 times

3

There is a way to configure the modelBuider, to lower my strings? For example, when I want all my data of type string, be at my bank a varchar, i make the following configuration:

 modelBuilder.Properties<string>()
                .Configure(p => p.HasColumnType("varchar"));

Is there any way to make these strings save all the lower case ones in my bank? and if there’s no way to modelbuider, what would be the most efficient way?

  • I’m pretty sure not, if you are, I wanted to understand what that means. It seems to me that you are just looking for a magic solution that has no meaning. The right way is to do it on hand when you need it..

  • The truth is that my application is saving everything in the way that the user type, so if it type like this :(Typing), it will save this way.. in case I would have to do the conversion for each given? Suppose I have a form with 100 fields, I have to make the transformation of each field 1 to 1 ?

  • Exactly that. What’s the problem? It’s quite true who doesn’t always work out, because you can normalize things that should have a different box anyway and you’ll kill that, you’re making decisions that shouldn’t be yours. If it’s some very specific field, okay, but if you’re going to do it from a generalized ma it’s almost certain that you’re doing something wrong. There are a lot of things you do one by one. I think I’ll answer in a little while.

1 answer

3


I went to do some research to make sure there was nothing frameworks Sometimes they like to do magic, although this case I think would be very pointless to do it. You really don’t have anything that can set this up, thank goodness.

As I already said in comment it makes no sense to connect this to all fields, or even do it manually at all. I’ve seen some software that does it and it’s horrible, distorting what the user has typed that is more able to determine what is right. In general it will normalize something that should not to prevent some possible abuse, if you want to hinder an abuse indicate that something is possibly abusing high cash, but let the user do it, if that is the case create a log, send an alert for any audit, but do not make a decision by the user when you are not able to do this.

If you have any rare case you want this it is usually more appropriate to treat on data entry, by mask or validation. In some case much more specific could do this in the model or database.

If you’re going to do it in the model then treat the property to do so, that is, create a logic that every time you assign a value in the property the box is normalized, something like that:

string descricao;
public string Descricao { get => descricao; set => descricao = value.ToLower(); }

I put in the Github for future reference.

Possibly there should be some other validation and normalization logic.

If you don’t want to do this you can customize the SaveChanges() of DbContext inheriting in a context of your own, but I doubt it’s any better than the above solution. If you do you treat the object there before saving.

And yes, in any case you need to do one by one, there is no magic. Programming is telling what each thing does. Of course, there is one thing that approaches magic, you can use reflection, a resource that can treat various things in a standardized way, but then you do all fields string act the same or assemble a system of notes to say that you want to do it in that field, which will probably give the same trouble to have done on the property as I demonstrated above, with a chance of something going wrong and getting much slower, which are the two main negative points of reflection, so do not.

The other possibility is to change the data manually, one by one, before saving, so you don’t have to create your own context or place the logic in the property, but this only makes sense if it is to perform the box normalization at some specific point of the code and not in the others.

Browser other questions tagged

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