-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mapping-aware cusom SQL #34245
Comments
This effectively seems to be a request for a type used to represent table/column names, i.e. a wrapper for string, just for the purpose of being able to use it with EF's FormattableString SQL query APIs. While I can see the theoretical argument here, I don't think we should introduce something like this to EF. My main problem is that it the FormattableString overloads have a very clem simple logic - anything that's interpolated becomes a DbParameter. Introducing something like the above feature starts blurring that, since some things would be doing (allegedly safe) string interpolation directly into the SQL, while other things would get parameterized as today. My recommendation in general is to:
|
While tests are obviously is a must, I won't need to fix the query every time migration affects naming. If I change property name in code, query will be changed automatically because property is referenced via expression.
Mixing parameters can be solved by using same used for structured logging:
Here formattable string is used only to interpolate db names, and parameters will be injected afterwards. Could you give me an example for the second item? How do I get the name of the column or table? Maybe I can create my custom extension atop of |
Such an overload doesn't currently exist, and introducing a new one would create lots of confusion with the existing API, where once again interpolation (curlies) means DbParameter. In the area of SQL querying, everything needs to be kept crystal clear and simple to understand, since otherwise it's very easy to fall into SQL injection.
You can easily get table and column names from the EF model, e.g.: var columnName = context.Model.FindEntityType(typeof(Blog)).GetProperty(nameof(Blog.Name)).GetColumnName(); I'll go ahead and close this as I don't see us implementing this - there isn't a problem that's important enough to be solved, there are other satisfactory options (use non-FormattableString overloads), and anything else would create lots of API confusion in a security-critical area. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
However, it's not escaped. I assume it's the responsibility of data provider. Is it possible to access this functionality? |
Tables/columns generally shouldn't need escaping - they're under the control of the application developer etc. But if you really want to, you can access the provider's quoting/escaping logic by resolving its ISqlGenerationHelper service. |
I'd like avoid writing custom sql query when I need to specify names of tables and columns.
Example:
If there's a migration which changes the name of the table or the column, this query will break.
As
ExecuteSqlAsync
expects an instance ofIFormattableString
it should be possible to write query the following way:First define tables and columns:
Next the query itself:
Table
andColumn
methods should return special type, so its values inIFormattableString
can be proccessed by the framework to produce correct string values.Yes, it does not protect me from specifying property from wrong table or putting parameter in the wrong place of SQL.
Yes, it's more verbose.
But it's still much safer.
The text was updated successfully, but these errors were encountered: