Is `NotParameterizedAttribute` working as expected?
See original GitHub issueI’m not sure whether it’s a bug report or just misunderstanding, but based on the attribute name and/or xmldocs, it should not be working correctly.
Consider the following setup:
// ...
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDbFunction(() => FromPostgis(default, default))
.HasTranslation(CustomSqlTranslatingExpressionVisitor.TranslateFromPostgis);
// ...
}
public virtual byte[] ToPostgis([NotParameterized] GeoFormat geoFormat, [NotParameterized] string data) =>
throw new NotSupportedException(_errorMessage);
We have such query:
var result = await _context.WithoutEntity
.Select(_ => new PlainGeometry
{
Format = query.Format,
Data = _context.FromPostgis(query.Format,
_context.ST_Difference(
_context.ST_MakeValid(query.Geometry),
_context.ST_MakeValid(query.CutterGeometry)))
})
.FirstOrDefaultAsync();
In the translation method, the very beginning:
public static SqlExpression TranslateFromPostgis(IReadOnlyCollection<SqlExpression> expressions)
{
var geoFormat = (GeoFormat) ((SqlConstantExpression) expressions.First()).Value;
// ...
}
The above throws InvalidCastException
because it’s not possible to cast from SqlParameterExpression
to SqlConstantExpression
. Since we need to have the value for our translation, we are forced to change most of our queries to never include the same variable/property twice in the same query (which is good enough for workaround, but we’d rather not do that), e.g.:
var result = await _context.WithoutEntity
.Select(_ => new PlainGeometry
{
Data = _context.FromPostgis(query.Format,
_context.ST_Difference(
_context.ST_MakeValid(query.Geometry),
_context.ST_MakeValid(query.CutterGeometry)))
})
.FirstOrDefaultAsync();
result.Format = query.Format;
As soon as the same variable or property is used multiple times in the same query, expression gets parameterized.
So my question is… Does the NotParameterizedAttribute
actually work as expected? Should it not force function argument to not be SqlParameterExpression
, regardless of whether the same value is used in multiple places?
Issue Analytics
- State:
- Created 2 years ago
- Comments:13 (6 by maintainers)
The attribute is supposed to be used when a closure dependent variable is used as a parameter and it shouldn’t be generated as a query parameter. There is no guarantee of it being SqlConstantExpression always. Without looking at repro code, we cannot say that it should be SqlConstantExpression always.
@smitpatel do we want to use this to track the previous bug (or deup it with another issue)?