Skip to content

Commit 390d2ac

Browse files
committed
#700 Add support for precision and scale to .NET 5. Thanks to statler.
1 parent 0481226 commit 390d2ac

16 files changed

+210
-52
lines changed

EntityFramework.Reverse.POCO.Generator/EF.Reverse.POCO.v3.ttinclude

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5493,8 +5493,10 @@
54935493
if (c.IsMaxLength)
54945494
sb.Append(".IsMaxLength()");
54955495

5496-
if ((c.Precision > 0 || c.Scale > 0) && c.PropertyType == "decimal")
5496+
if ((c.Precision > 0 || c.Scale > 0) && DatabaseReader.IsPrecisionAndScaleType(c.SqlPropertyType))
54975497
sb.AppendFormat(".HasPrecision({0},{1})", c.Precision, c.Scale);
5498+
else if (c.Precision > 0 && DatabaseReader.IsPrecisionType(c.SqlPropertyType) && c.SqlPropertyType != "float")
5499+
sb.AppendFormat(".HasPrecision({0})", c.Precision);
54985500

54995501
if (c.IsRowVersion)
55005502
sb.Append(".IsRowVersion()");
@@ -5766,6 +5768,14 @@
57665768
columnTypeParameters = $"({c.MaxLength})";
57675769

57685770
sb.AppendFormat(".HasColumnType(\"{0}{1}\")", c.SqlPropertyType, columnTypeParameters);
5771+
5772+
if (Settings.IsEfCore5Plus())
5773+
{
5774+
if ((c.Precision > 0 || c.Scale > 0) && DatabaseReader.IsPrecisionAndScaleType(c.SqlPropertyType))
5775+
sb.AppendFormat(".HasPrecision({0},{1})", c.Precision, c.Scale);
5776+
else if (c.Precision > 0 && DatabaseReader.IsPrecisionType(c.SqlPropertyType))
5777+
sb.AppendFormat(".HasPrecision({0})", c.Precision);
5778+
}
57695779
}
57705780

57715781
sb.Append(c.IsNullable ? ".IsRequired(false)" : ".IsRequired()");
@@ -5782,9 +5792,6 @@
57825792
//if (c.IsMaxLength)
57835793
// sb.Append(".IsMaxLength()");
57845794

5785-
//if ((c.Precision > 0 || c.Scale > 0) && c.PropertyType == "decimal")
5786-
// sb.AppendFormat(".HasPrecision({0},{1})", c.Precision, c.Scale);
5787-
57885795
if (c.IsRowVersion)
57895796
sb.Append(".IsRowVersion()");
57905797

@@ -6425,7 +6432,21 @@
64256432
public interface IDatabaseToPropertyType
64266433
{
64276434
Dictionary<string, string> GetMapping(); // [Database type] = Language type
6435+
6436+
/// <summary>
6437+
/// A list of the database types that are spatial.
6438+
/// </summary>
64286439
List<string> SpatialTypes();
6440+
6441+
/// <summary>
6442+
/// A list of the database types that contain precision.
6443+
/// </summary>
6444+
List<string> PrecisionTypes();
6445+
6446+
/// <summary>
6447+
/// A list of the database types that contain precision and scale.
6448+
/// </summary>
6449+
List<string> PrecisionAndScaleTypes();
64296450
}
64306451
public interface IDatabaseLanguageFactory
64316452
{
@@ -6582,6 +6603,16 @@
65826603
"geography", "geometry", "point", "linestring", "polygon", "multipoint", "multilinestring", "multipolygon", "geometrycollection"
65836604
};
65846605
}
6606+
6607+
public List<string> PrecisionTypes()
6608+
{
6609+
return new List<string> { "float", "datetime", "time", "timestamp", "year" };
6610+
}
6611+
6612+
public List<string> PrecisionAndScaleTypes()
6613+
{
6614+
return new List<string> { "decimal", "numeric" };
6615+
}
65856616
}
65866617

65876618
public class OracleToCSharp : IDatabaseToPropertyType
@@ -6625,6 +6656,16 @@
66256656
{
66266657
return new List<string> { "sdo_geometry" };
66276658
}
6659+
6660+
public List<string> PrecisionTypes()
6661+
{
6662+
return new List<string> { "float", "timestamp", "timestamp with time zone", "timestamp with local time zone" };
6663+
}
6664+
6665+
public List<string> PrecisionAndScaleTypes()
6666+
{
6667+
return new List<string> { "number" };
6668+
}
66286669
}
66296670

66306671
public class PostgresToCSharp : IDatabaseToPropertyType
@@ -6711,6 +6752,16 @@
67116752
{
67126753
return new List<string> { "geometry", "point", "line", "lseg", "box", "path", "polygon", "circle" };
67136754
}
6755+
6756+
public List<string> PrecisionTypes()
6757+
{
6758+
return new List<string> { "float" };
6759+
}
6760+
6761+
public List<string> PrecisionAndScaleTypes()
6762+
{
6763+
return new List<string> { "decimal", "numeric" };
6764+
}
67146765
}
67156766

67166767
public class SqlServerToCSharp : IDatabaseToPropertyType
@@ -6758,6 +6809,16 @@
67586809
{
67596810
return new List<string> { "geography", "geometry" };
67606811
}
6812+
6813+
public List<string> PrecisionTypes()
6814+
{
6815+
return new List<string> { "float", "datetime2", "datetimeoffset" };
6816+
}
6817+
6818+
public List<string> PrecisionAndScaleTypes()
6819+
{
6820+
return new List<string> { "decimal", "numeric" };
6821+
}
67616822
}
67626823

67636824
public class SqlServerToJavascript : IDatabaseToPropertyType
@@ -6802,6 +6863,16 @@
68026863
{
68036864
return new List<string> { "geography", "geometry" };
68046865
}
6866+
6867+
public List<string> PrecisionTypes()
6868+
{
6869+
return new List<string> { "float", "datetime2", "datetimeoffset" };
6870+
}
6871+
6872+
public List<string> PrecisionAndScaleTypes()
6873+
{
6874+
return new List<string> { "decimal", "numeric" };
6875+
}
68056876
}
68066877

68076878
public class DigitalSignaturePublic
@@ -11731,13 +11802,19 @@ and limitations under the License.
1173111802
protected Dictionary<string, string> StoredProcedureParameterDbType; // [SQL Data Type] = SqlDbType. (For consistent naming)
1173211803
protected Dictionary<string, string> DbTypeToPropertyType; // [SQL Data Type] = Language type.
1173311804
protected List<string> SpatialTypes;
11805+
protected List<string> PrecisionAndScaleTypes;
11806+
protected List<string> PrecisionTypes;
1173411807

1173511808
protected string DatabaseEdition, DatabaseEngineEdition, DatabaseProductVersion, DatabaseName;
1173611809
protected int DatabaseProductMajorVersion;
1173711810

1173811811
public bool IncludeSchema { get; protected set; }
1173911812
public bool DoNotSpecifySizeForMaxLength { get; protected set; }
1174011813

11814+
public bool IsSpatialType(string propertyType) => propertyType != null && SpatialTypes != null && SpatialTypes.Contains(propertyType);
11815+
public bool IsPrecisionAndScaleType(string propertyType) => propertyType != null && PrecisionAndScaleTypes != null && PrecisionAndScaleTypes.Contains(propertyType);
11816+
public bool IsPrecisionType(string propertyType) => propertyType != null && PrecisionTypes != null && PrecisionTypes.Contains(propertyType);
11817+
1174111818
protected abstract string TableSQL();
1174211819
protected abstract string ForeignKeySQL();
1174311820
protected abstract string ExtendedPropertySQL();
@@ -11774,6 +11851,8 @@ and limitations under the License.
1177411851

1177511852
DbTypeToPropertyType = databaseToPropertyType.GetMapping();
1177611853
SpatialTypes = databaseToPropertyType.SpatialTypes();
11854+
PrecisionTypes = databaseToPropertyType.PrecisionTypes();
11855+
PrecisionAndScaleTypes = databaseToPropertyType.PrecisionAndScaleTypes();
1177711856
DatabaseEdition = null;
1177811857
DatabaseEngineEdition = null;
1177911858
DatabaseProductVersion = null;
@@ -12201,7 +12280,7 @@ and limitations under the License.
1220112280
Precision = ChangeType<byte>(rdr["NUMERIC_PRECISION"]),
1220212281
Scale = ChangeType<int>(rdr["NUMERIC_SCALE"]),
1220312282
UserDefinedTypeName = rdr["USER_DEFINED_TYPE"].ToString().Trim(),
12204-
IsSpatial = SpatialTypes.Contains(dataType)
12283+
IsSpatial = IsSpatialType(dataType)
1220512284
};
1220612285

1220712286
if (string.IsNullOrEmpty(parameter.Name))

Generator.Tests.Unit/TestComparison/Northwind_SqlCe_Ef6_FkLegacy.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ public OrderConfiguration(string schema)
881881
Property(x => x.OrderDate).HasColumnName(@"Order Date").HasColumnType("datetime").IsOptional();
882882
Property(x => x.RequiredDate).HasColumnName(@"Required Date").HasColumnType("datetime").IsOptional();
883883
Property(x => x.ShippedDate).HasColumnName(@"Shipped Date").HasColumnType("datetime").IsOptional();
884-
Property(x => x.Freight).HasColumnName(@"Freight").HasColumnType("money").IsOptional().HasPrecision(19,4);
884+
Property(x => x.Freight).HasColumnName(@"Freight").HasColumnType("money").IsOptional();
885885

886886
// Foreign keys
887887
HasOptional(a => a.Employee).WithMany(b => b.Orders).HasForeignKey(c => c.EmployeeId).WillCascadeOnDelete(false); // Orders_FK02
@@ -905,7 +905,7 @@ public OrderDetailConfiguration(string schema)
905905

906906
Property(x => x.OrderId).HasColumnName(@"Order ID").HasColumnType("int").IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
907907
Property(x => x.ProductId).HasColumnName(@"Product ID").HasColumnType("int").IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
908-
Property(x => x.UnitPrice).HasColumnName(@"Unit Price").HasColumnType("money").IsRequired().HasPrecision(19,4);
908+
Property(x => x.UnitPrice).HasColumnName(@"Unit Price").HasColumnType("money").IsRequired();
909909
Property(x => x.Quantity).HasColumnName(@"Quantity").HasColumnType("smallint").IsRequired();
910910
Property(x => x.Discount).HasColumnName(@"Discount").HasColumnType("real").IsRequired();
911911

@@ -934,7 +934,7 @@ public ProductConfiguration(string schema)
934934
Property(x => x.ProductName).HasColumnName(@"Product Name").HasColumnType("nvarchar").IsRequired().HasMaxLength(40);
935935
Property(x => x.EnglishName).HasColumnName(@"English Name").HasColumnType("nvarchar").IsOptional().HasMaxLength(40);
936936
Property(x => x.QuantityPerUnit).HasColumnName(@"Quantity Per Unit").HasColumnType("nvarchar").IsOptional().HasMaxLength(20);
937-
Property(x => x.UnitPrice).HasColumnName(@"Unit Price").HasColumnType("money").IsOptional().HasPrecision(19,4);
937+
Property(x => x.UnitPrice).HasColumnName(@"Unit Price").HasColumnType("money").IsOptional();
938938
Property(x => x.UnitsInStock).HasColumnName(@"Units In Stock").HasColumnType("smallint").IsOptional();
939939
Property(x => x.UnitsOnOrder).HasColumnName(@"Units On Order").HasColumnType("smallint").IsOptional();
940940
Property(x => x.ReorderLevel).HasColumnName(@"Reorder Level").HasColumnType("smallint").IsOptional();

0 commit comments

Comments
 (0)