In Entity Framework Core, I can parameterize an SQL query like so:
_context.Database.ExecuteSqlCommandAsync($"select * from table where id = {myid}");
where the SQL query string is a FormattableString.
I need to run a SQL update for around 100 rows at a go and when I do using Linq, This makes 100 calls to the database, when I could easily do this using a SQL statement something like
UPDATE entity
SET column = CASE .....
in a single call. But I am not sure how to go about doing this for concatenated strings.
For example:
string sqlQuery = "UPDATE entity SET column = CASE "
for(int i = 0; i < 10; i++){
sqlQuery += "WHEN column2 = i THEN i + 1 ";
}
sqlQuery += "WHERE id IN (1,2,3,4,5,6,7,8,9,10)";
await _context.Database.ExecuteSqlCommandAsync(sqlQuery);
How can I sanitize or parameterize this query? Any help will be appreciated!