I am trying to dynamically construct a raw SQL query that will have X number of conditions. I am working from the info on this page: https://learn.microsoft.com/en-us/ef/core/querying/raw-sql
Currently I have something similar to this:
String rawQuery = "SELECT * FROM ItemsTable WHERE ";
foreach (f in FilterList) {
rawQuery = rawQuery + String.Format(f.condition, f.userInput);
// f.condition is something like "Name LIKE {0}"
}
var filteredItems = context.ItemsTable
.FromSql(rawQuery)
.ToList();
The problem is, my parameters are not being substituted in using .FromSql(), so I am vulnerable to SQL injection attacks.
Is there a way to use .FromSql() for this task?
OR, Is there another way I can protect against SQL injection?