如何配置实体框架以自动修剪值? [英] How can I configure Entity Framework to automatically trim values?
问题描述
我需要使"EF Core 2.1.0"从查询中的字符串字段中删除空格,"HasConversion"不起作用,您能告诉我为什么吗?
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()));
-使用DB2数据库和.net core 2.1
-using DB2 database and .net core 2.1
查询:
public List<ItemServico> List()
{
return _uow._db.ItensServico.ToList();
}
推荐答案
这就是varchar
类型的用途,可自动高效地修剪空间.
That's what the varchar
type is for, to trim spaces automatically, and efficiently.
手动trim()
操作每次都必须遍历字符串以找出有用的数据,而varchar
只是存储有用的长度.
Manual trim()
operations have to loop over the string every time to figure out the useful data, whereas varchar
simply stores the useful length.
通常,您会发现EF Core已删除了一些更无用的功能,以尽可能高效地实现有用的功能.尤其是更高版本的EF Core(落后3个版本).
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).
这篇关于如何配置实体框架以自动修剪值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!