RevEng: Always scaffold HasPrecision for decimal columns in SQL Server#37730
RevEng: Always scaffold HasPrecision for decimal columns in SQL Server#37730yykkibbb wants to merge 2 commits intodotnet:mainfrom
Conversation
|
@dotnet-policy-service agree |
63518f8 to
1044521
Compare
|
Hi @roji, @ajcvickers, @smitpatel, @bricelam — I'd be grateful if any of you could take a look at this PR when you have a chance. I'm fairly new to contributing to this project, but I'm eager to learn and do my best to help. If there's anything that needs to be adjusted or improved, I'm happy to make changes. Thank you so much for your time |
|
It's preferable to just generate |
|
@AndriySvyryd That will require you to depend on the Abstractions package |
|
@ErikEJ Sure, but that's already currently the case for non-default precision. Besides, we made that package very minimal precisely to allow it to be referenced from anywhere. |
1044521 to
cee2881
Compare
Move IScaffoldingTypeMapper, TypeScaffoldingInfo, and ScaffoldingTypeMapper from EFCore.Design to EFCore.Relational so that provider projects can extend them (following the AnnotationCodeGenerator pattern). Add ShouldAlwaysScaffoldPrecision virtual hook to ScaffoldingTypeMapper (returns false by default) that forces precision and scale to be scaffolded even when they match the default mapping. Create SqlServerScaffoldingTypeMapper overriding ShouldAlwaysScaffoldPrecision to return true for decimal, ensuring HasPrecision is always generated for decimal columns in SQL Server (preventing runtime warnings about unspecified decimal column types). Fixes dotnet#12241
cee2881 to
ce51049
Compare
|
@AndriySvyryd Thank you for the feedback! @ErikEJ Thank you for the helpful discussion as well! I've updated the approach to generate Changes:
Updated behavior:
|
|
Not sure if EF Core supports money, but the same issue applies to that, I think |
Verify that money and smallmoney columns are not affected by the ShouldAlwaysScaffoldPrecision change, since their store type does not match the default decimal mapping and HasColumnType is already scaffolded. Fixes dotnet#12241
|
@ErikEJ I checked the Since their store type is I've also added tests for both |
Summary
Make the "always scaffold
HasColumnTypefor decimal" behavior SQL Server provider-specific, rather than the current approach wheredecimal(18,2)(default precision/scale) produces no configuration — causing runtime warnings about unspecified decimal column types.Approach
Following the
AnnotationCodeGenerator/SqlServerAnnotationCodeGeneratorpattern:IScaffoldingTypeMapper,TypeScaffoldingInfo, andScaffoldingTypeMapperfromEFCore.DesigntoEFCore.Relationalso that provider projects can extend themShouldForceColumnType(Type)virtual hook toScaffoldingTypeMapper(returnsfalseby default), allowing providers to forceHasColumnTypescaffolding for specific CLR types even when the store type matches the defaultSqlServerScaffoldingTypeMapperthat overridesShouldForceColumnTypeto returntruefordecimal, and register it inSqlServerDesignTimeServicesBehavior change
decimal(18,2)(default)HasColumnType("decimal(18, 2)")decimal(14,3)(non-default)HasPrecision(14, 3)HasColumnType("decimal(14,3)")numeric(17,4)HasColumnType("numeric(17,4)")Fixes #12241