I tried to figure out how to set decimal precision for EF7 (Beta 4) without any luck.
I expected to do something like:
modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).Precision(10, 6)
This doesn't seem to be available, but I was able to find the following class in the repository on GitHub:
https://github.com/aspnet/EntityFramework/blob/7.0.0-beta4/src/EntityFramework.Relational/RelationalDecimalTypeMapping.cs
There are no examples of using RelationTypeMapping classes or method signatures with them. Maybe this is just used as part of the api mapping to retrieve information?
Another place I can expect is this:
modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).ForRelational().ColumnType()
or
modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).ForSqlServer().ColumnType()
It only requires a string, is this functionality simply not implemented or am I just not looking in the right place?
Edit: I just realized that the string is probably suitable for the type .ColumnType ("decimal (10.6)") until it is built further, but still not averse to getting some clarification, although since I would chose not to use strings for this
Edit: after clarifying from bricelam, I ended up creating the following extension, which needs to be used to avoid using a string, and I appreciate the simplicity of their approach:
public static RelationalPropertyBuilder DecimalPrecision(this RelationalPropertyBuilder propertyBuilder, int precision, int scale) { return propertyBuilder.ColumnType($"decimal({precision},{scale})"); }
Usage example:
modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).ForRelational().DecimalPrecision(10,6);
Edit: Making changes to RC1
I have not tested them yet, but I just put together the following 2 examples of how this will look like RC1
public static PropertyBuilder DecimalPrecision(this PropertyBuilder propertyBuilder, string precision, string scale) { return propertyBuilder.HasColumnType($"decimal({precision},{scale})"); } public static PropertyBuilder SqlDecimalPrecision(this PropertyBuilder propertyBuilder, string precision, string scale) { return propertyBuilder.ForSqlServerHasColumnType($"decimal({precision},{scale})"); }
Since I have not tried this yet, I am not sure what will be the correct use between "HasColumnType" or "ForSqlServerHasColumnType", but hopefully this will point someone in the right direction.