Skip to main content

Posts

Showing posts from June, 2024

EF CORE 6

 Enumerating through the results 3. Everytime we enumerate thorugh the DBset inside a foreach loop, the connection to the database remains open. So doing a lot of work inside the loop will result in the database connection to stay open. So, it's better to run a ToList() and get the  results in memory before we start iterating through the results. SQL Injection If we hardcode the value for a parameter in the LINQ query then it will be hardcoded in the query as well, so try to use variables. Variables are converted to parameters and hence prevent SQL injection.   Partial Filtering We can have more control on the LIKE statement when using the Ef.Functions.Like method  var authorsNameEndWith = await context.Authors.Where(a => EF.Functions.Like(a.FirstName, "%a")).ToListAsync(); Find  Order By If we have multiple order by statements, then LINQ will ignore all but the last one. So, use the thenBy operator to add more orderby statements. Triggering the query ex...