efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Add HasPrecision() Fluent API

Open bricelam opened this issue 8 years ago • 18 comments

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

bricelam avatar Apr 10 '18 20:04 bricelam

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.

slorion avatar Nov 02 '18 13:11 slorion

Is this gonna be shipped in 2.2.+?

whizkidwwe1217 avatar Jan 29 '19 14:01 whizkidwwe1217

@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.

ajcvickers avatar Jan 29 '19 17:01 ajcvickers

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

bricelam avatar Mar 22 '19 16:03 bricelam

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

bricelam avatar Mar 22 '19 16:03 bricelam

how to use it in "netcoreapp2.2"?

codeoflin avatar Sep 18 '19 23:09 codeoflin

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

whizkidwwe1217 avatar Sep 19 '19 01:09 whizkidwwe1217

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

smitpatel avatar Sep 19 '19 01:09 smitpatel

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

SARAVANA1501 avatar Sep 29 '19 09:09 SARAVANA1501

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

bricelam avatar Oct 01 '19 01:10 bricelam

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

bricelam avatar Oct 01 '19 01:10 bricelam

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

AndriySvyryd avatar Oct 01 '19 22:10 AndriySvyryd

@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 avatar Mar 28 '20 01:03 lajones

@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 avatar Mar 28 '20 01:03 AndriySvyryd

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

lajones avatar Mar 28 '20 05:03 lajones

Provider implementation should cast as needed.

AndriySvyryd avatar Mar 28 '20 06:03 AndriySvyryd

OK. Will do.

lajones avatar Mar 28 '20 06:03 lajones

@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.

lajones avatar Mar 28 '20 06:03 lajones