如何在ef core 2中包含不区分大小写的内容? [英] How do I make contains case-insensitive in ef core 2?
问题描述
我正在尝试通过搜索字符串过滤列表。它在蓝色的文档中显示请注意:
I am trying to filter a list by a search string. It says in the doc on the blue note that:
- IQueryable为您提供了
包含
的数据库提供程序实现。 -
IEnumerable
为您提供.NET Framework实现的包含
- SQL Server实例的默认设置不区分大小写。
- 使用
ToUpper
进行显式区分大小写-应该避免不敏感的调用,因为它会降低性能。
- IQueryable gives you the database provider implementation of
Contains
. IEnumerable
gives you the .NET Framework implementation ofContains
- The default setting of SQL Server instances is case-insensitive.
- Using
ToUpper
to make an explicit case-insensitive call should be avoided because it has a performance penalty.
我的过滤如下:
IQueryable<ApplicationUser> customers =
from u in _context.Users
where (u.Customer != null && u.IsActive)
select u;
if (!string.IsNullOrEmpty(searchString))
{
customers = customers.Where(s => s.Email.Contains(searchString));
}
但是,此解决方案区分大小写,我也不十分清楚为什么:因为我m使用 IQueryable
,它应该使用数据库提供程序实现,默认情况下不区分大小写,对吧?
This solution however is case-sensitive, and I don't really understand why: since I'm using IQueryable
, it should use the database provider implementation, that is case-insensitive by default, right?
我正在使用EF Core 2,并且当前仅运行本地MSSQLLocalDB。
I'm using EF Core 2 and currently just running a local MSSQLLocalDB.
推荐答案
从EF Core 2.1版本开始,您可以使用HasConversion( )。但是数据库中的信息将以小写形式存储:
starting from version 2.1 of the EF Core, you can use HasConversion(). But the information in the database will be stored in lowercase:
builder.Property(it => it.Email).HasConversion(v => v.ToLowerInvariant(), v => v);
我解决了类似的问题。这项更改解决了我所有的问题。
I solved a similar problem. This change solved all my problems.
这篇关于如何在ef core 2中包含不区分大小写的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!