I was reading the announcement of entity framework core 2.0 https://blogs.msdn.microsoft.com/dotnet/2017/08/14/announcing-entity-framework-core-2-0/
It says that they added new Sql functions like EF.Functions.Like
for performing the SQL LIKE
operation.
I was wondering, what then would be the difference between EF.Functions.Like
and string.Contains
/StartsWith
?
For example:
var customers = context.Customers.Where(c => c.Name.StartsWith("a")); // Version A var customers = context.Customers.Where(c => EF.Functions.Like(c.Name, "a%")); // Version B
What would be the difference between the two versions? EF already knows how to translate string.Contains
/StartsWith
to the corresponding SQL operations, doesn't it?
The only reason i can think of is that EF.Functions.Like would allow for more complex patterns like "a%b%"
(although this one can be written as StartsWith("a") && Contains("b")
)
Is this the reason?
Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.
EF Core 6.0 itself is 31% faster executing queries. Heap allocations have been reduced by 43%.
Entity Framework Classic Include The Include method lets you add related entities to the query result. In EF Classic, the Include method no longer returns an IQueryable but instead an IncludeDbQuery that allows you to chain multiple related objects to the query result by using the AlsoInclude and ThenInclude methods.
For one thing, EF Core does know not which case-sensitive or case-insensitive collation should be used. More importantly, applying a collation would in most cases prevent index usage, significantly impacting performance for a very basic and commonly-used .
Like query supports wildcard characters and hence very useful compared to the string extension methods in some scenarios.
For ex: If we were to search all the 4 lettered names with 'ri' as the middle characters we could do EF.Functions.Like(c.Name, "_ri_");
or to get all the customers from cities which start with vowels:
var customers = from c in context.Customers where EF.Functions.Like(c.City, "[aeiou]%") select c;
(Please read @Tseng's answer on how they are translated differently into SQL queries)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With