Skip to content

Commit d955e81

Browse files
committed
Get latest updates working with SqlCE
1 parent 1ee56b0 commit d955e81

File tree

4 files changed

+89
-69
lines changed

4 files changed

+89
-69
lines changed

EntityFramework.Reverse.POCO.Generator/Database NorthwindSqlCe40.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// Connection String Name: "MyDbContextSqlCE4"
1212
// Connection String: "Data Source=C:\S\Source (open source)\EntityFramework Reverse POCO Code Generator\EntityFramework.Reverse.POCO.Generator\App_Data\NorthwindSqlCe40.sdf"
1313
// ------------------------------------------------------------------------------------------------
14+
// Database Edition : SqlCE
15+
1416
// <auto-generated>
1517
// ReSharper disable ConvertPropertyToExpressionBody
1618
// ReSharper disable DoNotCallOverridableMethodsInConstructor

EntityFramework.Reverse.POCO.Generator/Database NorthwindSqlCe40.tt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// Main settings **********************************************************************************************************************
99
Settings.ConnectionStringName = "MyDbContextSqlCE4"; // Searches for this connection string in config files listed below in the ConfigFilenameSearchOrder setting
1010
// ConnectionStringName is the only required setting.
11-
Settings.CommandTimeout = 600; // SQL Command timeout in seconds. 600 is 10 minutes, 0 will wait indefinately. Some databases can be slow retrieving schema information.
1211
// As an alternative to ConnectionStringName above, which must match your app/web.config connection string name, you can override them below
1312
// Settings.ConnectionString = @"Data Source=C:\MyApplication\App_Data\NorthwindSqlCe40.sdf";
1413
// Settings.ProviderName = "System.Data.SqlServerCe.4.0";

EntityFramework.Reverse.POCO.Generator/Database.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
// Connection String Name: "MyDbContext"
1212
// Connection String: "Data Source=(local);Initial Catalog=northwind;Integrated Security=True;Application Name=EntityFramework Reverse POCO Generator"
1313
// ------------------------------------------------------------------------------------------------
14-
// Database Edition : Developer Edition (64-bit)
15-
// Database Engine Edition: Enterprise
14+
// Database Edition : Developer Edition (64-bit)
15+
// Database Engine Edition : Enterprise
16+
// Database Version : 13.0.4001.0
1617

1718
// <auto-generated>
1819
// ReSharper disable ConvertPropertyToExpressionBody

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

Lines changed: 84 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@
105105
public static bool UseMappingTables;
106106
public static bool UsePropertyInitializers;
107107
public static bool IsSqlCe;
108-
public static bool TemporalTableSupport;
109108
public static string FileExtension = ".cs";
110109
public static bool UsePascalCase;
111110
public static bool UsePrivateSetterForComputedColumns;
@@ -1069,7 +1068,7 @@
10691068
: "System.ComponentModel.DataAnnotations.Schema.";
10701069

10711070
bool isNewSequentialId = !string.IsNullOrEmpty(Default) && Default.ToLower().Contains("newsequentialid");
1072-
bool isTemporalColumn = this.GeneratedAlwaysType != ColumnGeneratedAlwaysType.NotApplicable;
1071+
bool isTemporalColumn = this.GeneratedAlwaysType != ColumnGeneratedAlwaysType.NotApplicable;
10731072

10741073
if (IsIdentity || isNewSequentialId || isTemporalColumn) // Identity, instead of Computed, seems the best for Temporal `GENERATED ALWAYS` columns: https://stackoverflow.com/questions/40742142/entity-framework-not-working-with-temporal-table
10751074
{
@@ -1672,7 +1671,7 @@
16721671
public abstract void ProcessForeignKeys(List<ForeignKey> fkList, Tables tables, bool checkForFkNameClashes);
16731672
public abstract void IdentifyForeignKeys(List<ForeignKey> fkList, Tables tables);
16741673
public abstract void ReadIndexes(Tables tables);
1675-
public abstract void ReadExtendedProperties(Tables tables, bool includeConnectionSettingComments, bool commentsInSummaryBlock);
1674+
public abstract void ReadExtendedProperties(Tables tables, bool commentsInSummaryBlock);
16761675

16771676
protected void WriteLine(string o)
16781677
{
@@ -1693,7 +1692,8 @@
16931692

16941693
private class SqlServerSchemaReader : SchemaReader
16951694
{
1696-
private static string _sqlDatabaseEdition, _sqlDatabaseEngineEdition;
1695+
private static string _sqlDatabaseEdition, _sqlDatabaseEngineEdition, _sqlDatabaseProductVersion;
1696+
private static int _sqlDatabaseProductMajorVersion;
16971697

16981698
private const string TableSQL = @"
16991699
SET NOCOUNT ON;
@@ -1717,12 +1717,12 @@ SELECT
17171717
ss.schema_id,
17181718
st.object_id AS table_object_id,
17191719
sv.object_id AS view_object_id,
1720-
1720+
17211721
sc.is_identity,
17221722
sc.is_rowguidcol,
17231723
sc.is_computed, -- Computed columns are read-only, do not confuse it with a column with a DEFAULT expression (which can be re-assigned). See the IsStoreGenerated attribute.
17241724
CONVERT( tinyint, [sc].[generated_always_type] ) AS generated_always_type -- SQL Server 2016 (13.x) or later. 0 = Not generated, 1 = AS_ROW_START, 2 = AS_ROW_END
1725-
1725+
17261726
INTO
17271727
#Columns
17281728
FROM
@@ -1744,7 +1744,7 @@ CREATE NONCLUSTERED INDEX IX_EfPoco_Columns
17441744
schema_id, table_object_id, view_object_id,
17451745
is_identity,is_rowguidcol,is_computed,generated_always_type
17461746
);
1747-
1747+
17481748
-----------
17491749

17501750
SELECT
@@ -1818,15 +1818,14 @@ SELECT
18181818
0
18191819
END
18201820
) AS IsStoreGenerated,
1821-
1821+
18221822
CONVERT( bit, ISNULL( pk.ORDINAL_POSITION, 0 ) ) AS PrimaryKey,
18231823
ISNULL(pk.ORDINAL_POSITION, 0) PrimaryKeyOrdinal,
18241824
CONVERT( bit, CASE WHEN fk.COLUMN_NAME IS NOT NULL THEN 1 ELSE 0 END ) AS IsForeignKey
18251825

18261826
FROM
1827-
18281827
#Columns c
1829-
1828+
18301829
LEFT OUTER JOIN #PrimaryKeys pk ON
18311830
c.TABLE_SCHEMA = pk.TABLE_SCHEMA AND
18321831
c.TABLE_NAME = pk.TABLE_NAME AND
@@ -1836,7 +1835,7 @@ FROM
18361835
c.TABLE_SCHEMA = fk.TABLE_SCHEMA AND
18371836
c.TABLE_NAME = fk.TABLE_NAME AND
18381837
c.COLUMN_NAME = fk.COLUMN_NAME
1839-
1838+
18401839
INNER JOIN INFORMATION_SCHEMA.TABLES t ON
18411840
c.TABLE_SCHEMA COLLATE DATABASE_DEFAULT = t.TABLE_SCHEMA COLLATE DATABASE_DEFAULT AND
18421841
c.TABLE_NAME COLLATE DATABASE_DEFAULT = t.TABLE_NAME COLLATE DATABASE_DEFAULT
@@ -1902,14 +1901,14 @@ FROM
19021901
u.TABLE_NAME,
19031902
u.COLUMN_NAME,
19041903
u.ORDINAL_POSITION
1905-
FROM
1904+
FROM
19061905
INFORMATION_SCHEMA.KEY_COLUMN_USAGE u
19071906
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc ON u.TABLE_SCHEMA = tc.CONSTRAINT_SCHEMA AND u.TABLE_NAME = tc.TABLE_NAME AND u.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
19081907
WHERE
19091908
CONSTRAINT_TYPE = 'PRIMARY KEY'
19101909
) pk
19111910
ON sc.NAME = pk.TABLE_SCHEMA AND sn.name = pk.TABLE_NAME AND c.name = pk.COLUMN_NAME
1912-
1911+
19131912
LEFT OUTER JOIN
19141913
(
19151914
SELECT DISTINCT
@@ -1950,7 +1949,7 @@ SELECT
19501949
c.is_computed AS IsComputed,
19511950
CONVERT( tinyint, [c].[generated_always_type] ) AS GeneratedAlwaysType,
19521951

1953-
CONVERT( bit,
1952+
CONVERT( bit,
19541953
CASE
19551954
WHEN
19561955
c.is_identity = 1 OR
@@ -2278,12 +2277,12 @@ SELECT '' AS SchemaName,
22782277
CASE WHEN c.DATA_TYPE = N'datetime' THEN 0 WHEN c.NUMERIC_SCALE IS NOT NULL THEN c.NUMERIC_SCALE ELSE 0 END AS Scale,
22792278

22802279
CAST(CASE WHEN c.AUTOINC_INCREMENT > 0 THEN 1 ELSE 0 END AS BIT) AS IsIdentity,
2281-
0 AS IsRowGuid,
2282-
0 AS IsComputed,
2280+
CONVERT( bit, 0 ) as IsComputed,
2281+
CONVERT( bit, 0 ) as IsRowGuid,
22832282
CONVERT( tinyint, 0 ) AS GeneratedAlwaysType,
22842283
CAST(CASE WHEN c.DATA_TYPE = N'rowversion' THEN 1 ELSE 0 END AS BIT) AS IsStoreGenerated,
2285-
CAST(CASE WHEN u.TABLE_NAME IS NULL THEN 0 ELSE 1 END AS BIT) AS PrimaryKey,
22862284
0 AS PrimaryKeyOrdinal,
2285+
CAST(CASE WHEN u.TABLE_NAME IS NULL THEN 0 ELSE 1 END AS BIT) AS PrimaryKey,
22872286
CONVERT( bit, 0 ) as IsForeignKey
22882287
FROM
22892288
INFORMATION_SCHEMA.COLUMNS c
@@ -2629,42 +2628,57 @@ OPTION (QUERYTRACEON 9481)";
26292628
return string.Empty;
26302629
}
26312630

2632-
private bool IsAzure(bool includeConnectionSettingComments)
2631+
private void ReadDatabaseEdition()
26332632
{
2634-
if (string.IsNullOrEmpty(_sqlDatabaseEdition))
2635-
{
2636-
if (Cmd == null)
2637-
return false;
2633+
if (Settings.IsSqlCe || !string.IsNullOrEmpty(_sqlDatabaseEdition))
2634+
return;
2635+
2636+
if (Cmd == null)
2637+
return;
26382638

2639-
Cmd.CommandText = @"
2639+
Cmd.CommandText = @"
26402640
SELECT SERVERPROPERTY('Edition') AS Edition,
2641-
CASE SERVERPROPERTY('EngineEdition')
2642-
WHEN 1 THEN 'Personal'
2643-
WHEN 2 THEN 'Standard'
2644-
WHEN 3 THEN 'Enterprise'
2645-
WHEN 4 THEN 'Express'
2646-
WHEN 5 THEN 'Azure'
2647-
ELSE 'Unknown'
2648-
END AS EngineEdition";
2641+
CASE SERVERPROPERTY('EngineEdition')
2642+
WHEN 1 THEN 'Personal'
2643+
WHEN 2 THEN 'Standard'
2644+
WHEN 3 THEN 'Enterprise'
2645+
WHEN 4 THEN 'Express'
2646+
WHEN 5 THEN 'Azure'
2647+
ELSE 'Unknown'
2648+
END AS EngineEdition,
2649+
SERVERPROPERTY('productversion') AS ProductVersion;";
26492650

2650-
Cmd.CommandTimeout = Settings.CommandTimeout;
2651+
Cmd.CommandTimeout = Settings.CommandTimeout;
26512652

2652-
using (DbDataReader rdr = Cmd.ExecuteReader())
2653+
using (DbDataReader rdr = Cmd.ExecuteReader())
2654+
{
2655+
if (rdr.Read())
26532656
{
2654-
if (rdr.Read())
2655-
{
2656-
_sqlDatabaseEdition = rdr["Edition"].ToString();
2657-
_sqlDatabaseEngineEdition = rdr["EngineEdition"].ToString();
2658-
if (includeConnectionSettingComments)
2659-
{
2660-
WriteLine("// Database Edition : " + _sqlDatabaseEdition);
2661-
WriteLine("// Database Engine Edition: " + _sqlDatabaseEngineEdition);
2662-
WriteLine("");
2663-
}
2664-
}
2657+
_sqlDatabaseEdition = rdr["Edition"].ToString();
2658+
_sqlDatabaseEngineEdition = rdr["EngineEdition"].ToString();
2659+
_sqlDatabaseProductVersion = rdr["ProductVersion"].ToString();
2660+
_sqlDatabaseProductMajorVersion = int.Parse(_sqlDatabaseProductVersion.Substring(0, 2).Replace(".",string.Empty));
26652661
}
26662662
}
2663+
}
26672664

2665+
private void WriteConnectionSettingComments()
2666+
{
2667+
if (Settings.IsSqlCe)
2668+
{
2669+
WriteLine("// Database Edition : SqlCE");
2670+
}
2671+
else
2672+
{
2673+
WriteLine("// Database Edition : " + _sqlDatabaseEdition);
2674+
WriteLine("// Database Engine Edition : " + _sqlDatabaseEngineEdition);
2675+
WriteLine("// Database Version : " + _sqlDatabaseProductVersion);
2676+
}
2677+
WriteLine("");
2678+
}
2679+
2680+
private bool IsAzure()
2681+
{
26682682
return _sqlDatabaseEngineEdition == "Azure";
26692683
}
26702684

@@ -2674,7 +2688,7 @@ SELECT SERVERPROPERTY('Edition') AS Edition,
26742688
{
26752689
return TableSQLCE;
26762690
}
2677-
2691+
26782692
String sql;
26792693
if( Settings.IncludeSynonyms )
26802694
{
@@ -2685,7 +2699,9 @@ SELECT SERVERPROPERTY('Edition') AS Edition,
26852699
sql = TableSQL + IncludeQueryTraceOn9481();
26862700
}
26872701

2688-
if (!Settings.TemporalTableSupport)
2702+
ReadDatabaseEdition();
2703+
var temporalTableSupport = _sqlDatabaseProductMajorVersion >= 13;
2704+
if (!temporalTableSupport)
26892705
{
26902706
// Replace the column names (only present in SQL Server 2016 or later) with literal constants so the query works with older versions of SQL Server.
26912707
sql = sql
@@ -2699,6 +2715,9 @@ SELECT SERVERPROPERTY('Edition') AS Edition,
26992715

27002716
public override Tables ReadSchema()
27012717
{
2718+
ReadDatabaseEdition();
2719+
WriteConnectionSettingComments();
2720+
27022721
var result = new Tables();
27032722
if (Cmd == null)
27042723
return result;
@@ -2790,7 +2809,7 @@ SELECT SERVERPROPERTY('Edition') AS Edition,
27902809
}
27912810

27922811
if (Settings.IncludeExtendedPropertyComments != CommentsStyle.None)
2793-
ReadExtendedProperties(result, Settings.IncludeConnectionSettingComments, Settings.IncludeExtendedPropertyComments == CommentsStyle.InSummaryBlock);
2812+
ReadExtendedProperties(result, Settings.IncludeExtendedPropertyComments == CommentsStyle.InSummaryBlock);
27942813

27952814
ReadIndexes(result);
27962815

@@ -2953,7 +2972,7 @@ SELECT SERVERPROPERTY('Edition') AS Edition,
29532972
}
29542973
}
29552974

2956-
public override void ReadExtendedProperties(Tables tables, bool includeConnectionSettingComments, bool commentsInSummaryBlock)
2975+
public override void ReadExtendedProperties(Tables tables, bool commentsInSummaryBlock)
29572976
{
29582977
if(Cmd == null)
29592978
return;
@@ -2969,7 +2988,7 @@ SELECT SERVERPROPERTY('Edition') AS Edition,
29692988
}
29702989
else
29712990
{
2972-
if (IsAzure(includeConnectionSettingComments))
2991+
if (IsAzure())
29732992
return;
29742993

29752994
Cmd.CommandText = ExtendedPropertySQL + IncludeQueryTraceOn9481();
@@ -3025,7 +3044,7 @@ SELECT SERVERPROPERTY('Edition') AS Edition,
30253044
if (Cmd.GetType().Name == "SqlCeCommand")
30263045
return result;
30273046

3028-
if (IsAzure(Settings.IncludeConnectionSettingComments))
3047+
if (IsAzure())
30293048
Cmd.CommandText = StoredProcedureSQLAzure + IncludeQueryTraceOn9481();
30303049
else if (Settings.IncludeSynonyms)
30313050
Cmd.CommandText = SynonymStoredProcedureSQLSetup + StoredProcedureSQL + SynonymStoredProcedureSQL + IncludeQueryTraceOn9481();
@@ -3397,21 +3416,20 @@ SELECT SERVERPROPERTY('Edition') AS Edition,
33973416
if(rdr == null)
33983417
throw new ArgumentNullException("rdr");
33993418

3400-
string typename = rdr["TypeName"].ToString().Trim().ToLower();
3401-
Int32 rdrScale = (int)rdr["Scale"];
3402-
3403-
Boolean rdrIsNullable = (Boolean)rdr["IsNullable"];
3404-
Int32 rdrMaxLength = (int)rdr["MaxLength"];
3405-
Int32 rdrDtp = (int)rdr["DateTimePrecision"];
3406-
Int32 rdrPrecision = (int)rdr["Precision"];
3407-
Boolean rdrIsIdentity = (Boolean)rdr["IsIdentity"];
3408-
Boolean rdrIsComputed = (Boolean)rdr["IsComputed"];
3409-
Boolean rdrIsRowGuid = (Boolean)rdr["IsRowGuid"];
3410-
Byte rdrGat = (Byte)rdr["GeneratedAlwaysType"];
3411-
Boolean rdrIsg = (Boolean)rdr["IsStoreGenerated"];
3412-
Int32 rdrPko = (int)rdr["PrimaryKeyOrdinal"];
3413-
Boolean rdrIsPk = (Boolean)rdr["PrimaryKey"];
3414-
Boolean rdrIsFk = (Boolean)rdr["IsForeignKey"];
3419+
string typename = rdr["TypeName"].ToString().Trim().ToLower();
3420+
int rdrScale = (int)rdr["Scale"];
3421+
bool rdrIsNullable = (bool)rdr["IsNullable"];
3422+
int rdrMaxLength = (int)rdr["MaxLength"];
3423+
int rdrDtp = (int)rdr["DateTimePrecision"];
3424+
int rdrPrecision = (int)rdr["Precision"];
3425+
bool rdrIsIdentity = (bool)rdr["IsIdentity"];
3426+
bool rdrIsComputed = (bool)rdr["IsComputed"];
3427+
bool rdrIsRowGuid = (bool)rdr["IsRowGuid"];
3428+
byte rdrGat = (byte)rdr["GeneratedAlwaysType"];
3429+
bool rdrIsg = (bool)rdr["IsStoreGenerated"];
3430+
int rdrPko = (int)rdr["PrimaryKeyOrdinal"];
3431+
bool rdrIsPk = (bool)rdr["PrimaryKey"];
3432+
bool rdrIsFk = (bool)rdr["IsForeignKey"];
34153433

34163434
var col = new Column
34173435
{
@@ -4434,4 +4452,4 @@ SELECT SERVERPROPERTY('Edition') AS Edition,
44344452
}
44354453
}
44364454

4437-
#>
4455+
#>

0 commit comments

Comments
 (0)