-
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
Default Size behavior in RelationalTypeMapping may be incorrect #11591
Comments
@roji The code for setting the size in SQL Server is there for a number of reasons:
The key part about the first point is to use a small number of size values for parameters and to use the correct size whenever possible. Therefore:
On the other hand, the default behavior for parameters is to set size only if the size has been specified, otherwise do nothing. This seemed like a good default behavior given that different ADO.NET providers can behave differently and have different requirements for size. So my question is, based on your experience, which parts of the SQL Server algorithm do you think are common across providers? For example, does Postgres ever require the size to be set? If so, what is the impact of setting the size? |
@roji Any thoughts on this? |
It'll take me a few more days to respond, sorry... |
On the PostgreSQL side, there's not a whole lot of usage for length. Unless I'm mistaken, the only column types where length is allowed are text and bitstring. Actually specifying the size isn't required: On the client side, as I wrote before, setting To summarize, if you don't want to stand in the way of server validation of length (which is how your SQL Server provider works at the moment), then it may be a good idea to stop setting |
Thanks @roji. We decided in triage that we will make the change in 2.2 to never set Size by default. SQL Server will continue to override for its additional logic. Currently Oracle, MySQL and SqlCompact also all override and so will not be impacted by the change. It will cause Postgres and SQLite to stop silently truncating, which could be considered a breaking change, but we are choosing to view as a bug fix. You may want to override in the Postgres provider for 2.1--it's a safer change to make there at this point than it is to make it in the core code. |
We used to have our special NpgsqlStringTypeMapping, extending StringTypeMapping, in order to override the Size behavior of the latter. The problematic was removed in 2.2.0 (see dotnet/efcore#11591), so we no longer need this.
We used to have our special NpgsqlStringTypeMapping, extending StringTypeMapping, in order to override the Size behavior of the latter. The problematic was removed in 2.2.0 (see dotnet/efcore#11591), so we no longer need this.
Originally raised in npgsql/efcore.pg#357.
The default behavior in RelationalTypeMapping is to set the parameter's Size to the mapping's Size, if one is specified. Since DbParameter.Size means truncating the value, that means that by default EF Core tells the underlying ADO.NET provider to perform client-side truncation based on the mapping's size. This behavior isn't overridden in StringTypeMapping.
However, the SqlServer provider does override this behavior for strings and for byte arrays. Both seem to set DbParameter.Size to either maxSpecificSize (if the value length is smaller than then max column length) or to -1. This way, the parameter data is never truncated and the backend returns an error as expected.
If this is the behavior that you're looking for across providers (and I see no reason for this to vary), then I think you may want to change RelationalTypeMapping to no longer truncate by default.
The text was updated successfully, but these errors were encountered: