Efcore: Add HasPrecision() Fluent API

Created on 10 Apr 2018  路  18Comments  路  Source: dotnet/efcore

Our type mapping supports precision and scale. We should add back EF6's HasPrecision() method on PropertyBuilder.

area-model-building closed-fixed punted-for-3.0 type-enhancement

Most helpful comment

@whizkidwwe1217 If you mean in a patch of 2.2, then no. If you mean in 3.0 or later, then that is the plan.

All 18 comments

Any ETA on this feature? Or any way to do it in a database agnostic way currently? Having to tie up model creation to a particular database solely because of this missing piece is quite annoying.

Is this gonna be shipped in 2.2.+?

@whizkidwwe1217 If you mean in a patch of 2.2, then no. If you mean in 3.0 or later, then that is the plan.

Note to implementer: Don't forget to...

  • Flow these through MigrationsSqlGenerator.GetColumnType()
  • Add them to ColumnOperation, MigrationsBuilder.Add/AlterColumn, and ColumnsBuilder.Column
  • Serialize it into ModelSnapshot
  • Diff it in MigrationsModelDiffer

SQL Server notes:

Data type | Default precision | Default scale
--- | ---:| ---:
datetime2 | 7 | (n/a)
datetimeoffset | 7 | (n/a)
decimal | 18 | 0
float | 53 | (n/a)
numeric | 18 | 0
time | (n/a) | 7

how to use it in "netcoreapp2.2"?

I think they're planning to implement it in 3.0 but we really need this in 2.2. @ajcvickers @bricelam

You can always specify full type using HasColumnType. This would be a good sugar to add but not a blocker in any sense.

@bricelam I am interested in working on it, Will you be able to provide more implementation details?

Look at the IsUnicode or HasMaxLength code for an end-to-end example

And yes, feel free to open a draft PR if you get stuck or have implementation questions

@SARAVANA1501 RelationalTypeMappingSource should also be updated to account for this.

@dotnet/efcore Throughout a lot of the current code precision and scale are treated as ints. But EF6's HasPrecision(precision, scale) treated them as bytes. and so does DbParameter.
Should I update all our code to use byte or simply cast to byte when I need a DbParameter? My preference is the former but wanted to make sure because it's in a lot of places.

@lajones This API is defined in Core, so DbParameter isn't that relevant. We usually stick to int unless there's a good reason not to even if the value will never be larger than Byte.Max

@AndriySvyryd I'm updating SqlServerDecimalTypeMapping.ConfigureParameter to include precision and scale. Currently Precision.Value is an int?, but parameter.Precision requires a byte.

Provider implementation should cast as needed.

OK. Will do.

@dotnet/efcore Also would like opinion on what effect HasPrecision(precision) with no scale specified should have on the scale.
SqlServerDecimalTypeMapping's default for a decimal property is decimal(18,2), in particular note that the scale is 2. If I then call e.g. HasPrecision(4) on that property, i.e. I do not specify the scale, should I expect the scale to remain unchanged at 2 or should it change to 0? If you define a column in SqlServer as decimal(4) you expect the precision to be 4 and the scale to be 0, i.e. any time I call just HasPrecision(precision) with no scale then the scale is set to 0. But remember HasPrecision() applies to all providers, not just SqlServer.

Was this page helpful?
0 / 5 - 0 ratings