Entity Framework Core introduced a static library that holds database provider specific methods. The first implementation is the SQL Server Like operator. Phil demonstrates using the EF Like operator in LINQ.
- [Instructor] EF Core 2.0 introduced a new DB functions object on the aesthetic EF class. This serves as a container for data store specific features. The first and only feature released at this time is the Like operator for SQL Server, but I'm sure more will be coming soon. And the Like operator does exactly what you would think, it generates a T-SQL-like operation in the generated query. Now it is important to note that you must add the percent operators yourself.
Alright, let's search for some products based on part of the product name. So we'll say var products equals context. product .where X. Name. Contains. Crankarm .To list.
And we'll just put in a search .equal Alright. So we're looking for all the products where the name contains that string. Another usual type of query to run, so let's run this. And this works by the way. You can certainly do this method and it runs server side.
Here we go. And let's look at the results. So it's using the CHARINDEX function in SQL Server. So that's anywhere in a name. What if we want to specify the first part of the name, or the end part of the name? We could do begins with, ends with, those types of string functions. But we're on the verge of being dangerously close to running client side. And the code gets a little messy. It just feels better to use a Like operator in that sense because how we write the T-SQL naturally.
So let's go back into our tests. And I'm going to copy this, but I'm going to change the where to instead of saying contains crankarm, I'm going to say EF. Functions .Like. And we have to have the property. And then what we're looking for. And you notice how the Intellisense says it's a pattern.
And what that pattern means is we have to add in the percent signs ourself. Let's this query and see what it looks like in T-SQL. Much better. Much more what we would expect. So very simply, using the EF. Functions Like operator, we have a more natural way to search for strings within properties. Again, the Like operator is only DB Function available at this time, but more will be implemented and they will be database provider specific.
- Entity Framework Core components and projects
- Working with scaffolded files
- Testing with xUnit
- Viewing generated SQL
- Composing queries
- Sorting and filtering results
- Working with aggregates
- Loading related data
- Logging and tracking
- Mapping functions
- Generics and delegates
- Checking concurrency
- Resiliency and transactions