@@ -20,35 +20,12 @@ namespace Microsoft.EntityFrameworkCore.SqlServer.Scaffolding.Internal;
2020/// any release. You should only use it directly in your code with extreme caution and knowing that
2121/// doing so can result in application failures when updating to a new Entity Framework Core release.
2222/// </summary>
23- public class SqlServerDatabaseModelFactory : DatabaseModelFactory
23+ public class SqlServerDatabaseModelFactory (
24+ IDiagnosticsLogger < DbLoggerCategory . Scaffolding > logger ,
25+ IRelationalTypeMappingSource typeMappingSource ) : DatabaseModelFactory
2426{
25- private readonly IDiagnosticsLogger < DbLoggerCategory . Scaffolding > _logger ;
26- private readonly IRelationalTypeMappingSource _typeMappingSource ;
27-
28- private static readonly ISet < string > DateTimePrecisionTypes = new HashSet < string >
29- {
30- "datetimeoffset" ,
31- "datetime2" ,
32- "time"
33- } ;
34-
35- private static readonly ISet < string > MaxLengthRequiredTypes
36- = new HashSet < string >
37- {
38- "binary" ,
39- "varbinary" ,
40- "char" ,
41- "varchar" ,
42- "nchar" ,
43- "nvarchar"
44- } ;
45-
46- private enum EngineEdition
47- {
48- SqlDataWarehouse = 6 ,
49- SqlOnDemand = 11 ,
50- DynamicsTdsEndpoint = 1000 ,
51- }
27+ private readonly IDiagnosticsLogger < DbLoggerCategory . Scaffolding > _logger = logger ;
28+ private readonly IRelationalTypeMappingSource _typeMappingSource = typeMappingSource ;
5229
5330 private const string NamePartRegex
5431 = @"(?:(?:\[(?<part{0}>(?:(?:\]\])|[^\]])+)\])|(?<part{0}>[^\.\[\]]+))" ;
@@ -63,35 +40,10 @@ private static readonly Regex PartExtractor
6340 RegexOptions . Compiled ,
6441 TimeSpan . FromMilliseconds ( 1000 ) ) ;
6542
66- // see https://msdn.microsoft.com/en-us/library/ff878091.aspx
67- // decimal/numeric are excluded because default value varies based on the precision.
68- private static readonly Dictionary < string , long [ ] > DefaultSequenceMinMax =
69- new ( StringComparer . OrdinalIgnoreCase )
70- {
71- { "tinyint" , [ 0L , 255L ] } ,
72- { "smallint" , [ - 32768L , 32767L ] } ,
73- { "int" , [ - 2147483648L , 2147483647L ] } ,
74- { "bigint" , [ - 9223372036854775808L , 9223372036854775807L ] }
75- } ;
76-
7743 private byte ? _compatibilityLevel ;
7844 private EngineEdition ? _engineEdition ;
7945 private string ? _version ;
8046
81- /// <summary>
82- /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
83- /// the same compatibility standards as public APIs. It may be changed or removed without notice in
84- /// any release. You should only use it directly in your code with extreme caution and knowing that
85- /// doing so can result in application failures when updating to a new Entity Framework Core release.
86- /// </summary>
87- public SqlServerDatabaseModelFactory (
88- IDiagnosticsLogger < DbLoggerCategory . Scaffolding > logger ,
89- IRelationalTypeMappingSource typeMappingSource )
90- {
91- _logger = logger ;
92- _typeMappingSource = typeMappingSource ;
93- }
94-
9547 /// <summary>
9648 /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
9749 /// the same compatibility standards as public APIs. It may be changed or removed without notice in
@@ -488,13 +440,24 @@ FROM [sys].[sequences] AS [s]
488440 MaxValue = maxValue
489441 } ;
490442
491- if ( DefaultSequenceMinMax . TryGetValue ( storeType , out var defaultMinMax ) )
443+ // See https://msdn.microsoft.com/en-us/library/ff878091.aspx
444+ // decimal/numeric are excluded because the default value varies based on the precision.
445+ var ( defaultMin , defaultMax ) = storeType . ToLowerInvariant ( ) switch
446+ {
447+ "tinyint" => ( 0L , 255L ) ,
448+ "smallint" => ( - 32768L , 32767L ) ,
449+ "int" => ( - 2147483648L , 2147483647L ) ,
450+ "bigint" => ( - 9223372036854775808L , 9223372036854775807L ) ,
451+
452+ _ => ( ( long ? ) null , ( long ? ) null )
453+ } ;
454+
455+ if ( defaultMin is not null && defaultMax is not null )
492456 {
493- var defaultMin = defaultMinMax [ 0 ] ;
494457 sequence . MinValue = sequence . MinValue == defaultMin ? null : sequence . MinValue ;
495458 sequence . StartValue = sequence . StartValue == defaultMin ? null : sequence . StartValue ;
496459
497- sequence . MaxValue = sequence . MaxValue == defaultMinMax [ 1 ]
460+ sequence . MaxValue = sequence . MaxValue == defaultMax
498461 ? null
499462 : sequence . MaxValue ;
500463 }
@@ -1016,13 +979,14 @@ private static string GetStoreType(string dataTypeName, int maxLength, int preci
1016979 return $ "vector({ vectorDimensions } )";
1017980 }
1018981
1019- if ( DateTimePrecisionTypes . Contains ( dataTypeName )
1020- && scale != 7 )
982+ // date/time types with scale facet
983+ if ( dataTypeName is "time" or "datetime2" or "datetimeoffset" && scale != 7 )
1021984 {
1022985 return $ "{ dataTypeName } ({ scale } )";
1023986 }
1024987
1025- if ( MaxLengthRequiredTypes . Contains ( dataTypeName ) )
988+ // These types require a length facet
989+ if ( dataTypeName is "binary" or "varbinary" or "char" or "varchar" or "nchar" or "nvarchar" )
1026990 {
1027991 if ( maxLength == - 1 )
1028992 {
@@ -1513,19 +1477,19 @@ private bool SupportsSequences()
15131477 => _compatibilityLevel >= 110 && IsFullFeaturedEngineEdition ( ) ;
15141478
15151479 private bool SupportsIndexes ( )
1516- => _engineEdition != EngineEdition . DynamicsTdsEndpoint ;
1480+ => _engineEdition != EngineEdition . DynamicsCrm ;
15171481
15181482 private bool SupportsVectorIndexes ( )
1519- => _compatibilityLevel >= 170 ;
1483+ => _compatibilityLevel >= 170 && IsFullFeaturedEngineEdition ( ) ;
15201484
15211485 private bool SupportsViews ( )
1522- => _engineEdition != EngineEdition . DynamicsTdsEndpoint ;
1486+ => _engineEdition != EngineEdition . DynamicsCrm ;
15231487
15241488 private bool SupportsTriggers ( )
15251489 => IsFullFeaturedEngineEdition ( ) ;
15261490
15271491 private bool IsFullFeaturedEngineEdition ( )
1528- => _engineEdition is not EngineEdition . SqlDataWarehouse and not EngineEdition . SqlOnDemand and not EngineEdition . DynamicsTdsEndpoint
1492+ => _engineEdition is not EngineEdition . AzureSynapseAnalytics and not EngineEdition . AzureSynapseServerlessOrFabric and not EngineEdition . DynamicsCrm
15291493 && _version != "Microsoft SQL Kusto" ;
15301494
15311495 private static string DisplayName ( string ? schema , string name )
@@ -1540,4 +1504,63 @@ private static string DisplayName(string? schema, string name)
15401504 "SET_DEFAULT" => ReferentialAction . SetDefault ,
15411505 _ => null
15421506 } ;
1507+
1508+ // See https://learn.microsoft.com/sql/t-sql/functions/serverproperty-transact-sql
1509+ private enum EngineEdition
1510+ {
1511+ /// <summary>
1512+ /// Personal or Desktop Engine (Not available in SQL Server 2005 (9.x) and later versions.)
1513+ /// </summary>
1514+ PersonalOrDesktop = 1 ,
1515+
1516+ /// <summary>
1517+ /// Standard (For Standard, Standard Developer, Web, and Business Intelligence.)
1518+ /// </summary>
1519+ Standard = 2 ,
1520+
1521+ /// <summary>
1522+ /// Enterprise (For Enterprise, Enterprise Developer, Developer, and Evaluation editions.)
1523+ /// </summary>
1524+ Enterprise = 3 ,
1525+
1526+ /// <summary>
1527+ /// Express (For Express, Express with Tools, and Express with Advanced Services)
1528+ /// </summary>
1529+ Express = 4 ,
1530+
1531+ /// <summary>
1532+ /// SQL Database (Azure SQL Database)
1533+ /// </summary>
1534+ SqlDatabase = 5 ,
1535+
1536+ /// <summary>
1537+ /// Azure Synapse Analytics
1538+ /// </summary>
1539+ AzureSynapseAnalytics = 6 ,
1540+
1541+ /// <summary>
1542+ /// Azure SQL Managed Instance
1543+ /// </summary>
1544+ AzureSqlManaged = 8 ,
1545+
1546+ /// <summary>
1547+ /// Azure SQL Edge (For all editions of Azure SQL Edge)
1548+ /// </summary>
1549+ AzureSqlEdge = 9 ,
1550+
1551+ /// <summary>
1552+ /// Azure Synapse serverless SQL pool, or Microsoft Fabric
1553+ /// </summary>
1554+ AzureSynapseServerlessOrFabric = 11 ,
1555+
1556+ /// <summary>
1557+ /// Microsoft Fabric SQL database in Microsoft Fabric.
1558+ /// </summary>
1559+ FabricSql = 12 ,
1560+
1561+ /// <summary>
1562+ /// Dynamics CRM TDS endpoint
1563+ /// </summary>
1564+ DynamicsCrm = 1000 ,
1565+ }
15431566}
0 commit comments