How can I configure Entity Framework to automatically trim values?

自古美人都是妖i 提交于 2020-06-22 04:25:08

问题


I need to make “EF Core 2.1.0” remove white space from Strings fields in queries, “HasConversion” not is not working, can you tell me why?

entity.Property(e => e.Name)
             .HasConversion(
                new ValueConverter<string, string>(v => v.TrimEnd(), v => v.TrimEnd()));

-using DB2 database and .net core 2.1

Query:

public List<ItemServico> List()
        {
            return _uow._db.ItensServico.ToList();
        }

回答1:


That's what the varchar type is for, to trim spaces automatically, and efficiently.

Manual trim() operations have to loop over the string every time to figure out the useful data, whereas varchar simply stores the useful length.

In general, you'll find that EF Core has moved to remove the more useless functions in an effort to implement the useful functions in as efficient as manner as possible. Especially later EF Core versions (you're 3 versions behind).



来源:https://stackoverflow.com/questions/62263948/how-can-i-configure-entity-framework-to-automatically-trim-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!