From d1927f17e39e1009954f6ad44b7fca7480ef39d2 Mon Sep 17 00:00:00 2001 From: Tim Abell Date: Tue, 18 Aug 2015 07:44:47 +0100 Subject: [PATCH 1/8] typo fix - description sql parameter shouldn't affect anything Original patch by Svein Helge #mailto:svein.helge.grinden@gmail.comin PR #33 --- EfEnumToLookupTests/Tests/TestStuff.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/EfEnumToLookupTests/Tests/TestStuff.cs b/EfEnumToLookupTests/Tests/TestStuff.cs index 3bbe946..d8f10b0 100644 --- a/EfEnumToLookupTests/Tests/TestStuff.cs +++ b/EfEnumToLookupTests/Tests/TestStuff.cs @@ -63,9 +63,9 @@ public void UsesDescriptionAttribute() { using (var context = new MagicContext()) { - const string sql = "select @desciption = name from Enum_Importance where id = @id"; + const string sql = "select @description = name from Enum_Importance where id = @id"; var idParam = new SqlParameter("id", (int)Importance.NotBovverd); - var outParam = new SqlParameter("desciption", SqlDbType.NVarChar, 255) { Direction = ParameterDirection.Output }; + var outParam = new SqlParameter("description", SqlDbType.NVarChar, 255) { Direction = ParameterDirection.Output }; context.Database.ExecuteSqlCommand(sql, idParam, outParam); var actualName = outParam.Value; Assert.AreEqual(Constants.BovveredDisplay, actualName); From 182a9b8c0b156e121cea677fe164888006dc11d1 Mon Sep 17 00:00:00 2001 From: Tim Abell Date: Tue, 18 Aug 2015 07:46:46 +0100 Subject: [PATCH 2/8] code formatting taken from PR #33 --- ExampleUsage/EnumExample.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ExampleUsage/EnumExample.cs b/ExampleUsage/EnumExample.cs index 38e6974..e94252a 100644 --- a/ExampleUsage/EnumExample.cs +++ b/ExampleUsage/EnumExample.cs @@ -39,7 +39,8 @@ public void ExampleOfUsingApply() } } - [Test] public void ExampleOfGeneratingSql() + [Test] + public void ExampleOfGeneratingSql() { using (var context = new MyDbContext()) { From fb4c67d6558f44392c874878de5387031991abe5 Mon Sep 17 00:00:00 2001 From: Tim Abell Date: Tue, 18 Aug 2015 07:51:25 +0100 Subject: [PATCH 3/8] code-doc - remove misleading info about defaults defaults are set by the caller not by the implementation fixes #36 --- EfEnumToLookup/LookupGenerator/SqlServerHandler.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/EfEnumToLookup/LookupGenerator/SqlServerHandler.cs b/EfEnumToLookup/LookupGenerator/SqlServerHandler.cs index 7830b6b..6de1668 100644 --- a/EfEnumToLookup/LookupGenerator/SqlServerHandler.cs +++ b/EfEnumToLookup/LookupGenerator/SqlServerHandler.cs @@ -9,21 +9,19 @@ class SqlServerHandler : IDbHandler { /// /// The size of the Name field that will be added to the generated lookup tables. - /// Adjust to suit your data if required, defaults to 255. + /// Adjust to suit your data if required. /// public int NameFieldLength { get; set; } /// /// Prefix to add to all the generated tables to separate help group them together /// and make them stand out as different from other tables. - /// Defaults to "Enum_" set to null or "" to not have any prefix. /// public string TableNamePrefix { get; set; } /// /// Suffix to add to all the generated tables to separate help group them together /// and make them stand out as different from other tables. - /// Defaults to "" set to null or "" to not have any suffix. /// public string TableNameSuffix { get; set; } From 4b98c07e36a00ec230d3b5c3a574703def35a439 Mon Sep 17 00:00:00 2001 From: Tim Abell Date: Tue, 18 Aug 2015 08:12:09 +0100 Subject: [PATCH 4/8] code-doc - IDbHandler * more info * copy method documentation to sql server implementation --- EfEnumToLookup/LookupGenerator/IDbHandler.cs | 3 ++- EfEnumToLookup/LookupGenerator/SqlServerHandler.cs | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/EfEnumToLookup/LookupGenerator/IDbHandler.cs b/EfEnumToLookup/LookupGenerator/IDbHandler.cs index c26c47c..3bbc790 100644 --- a/EfEnumToLookup/LookupGenerator/IDbHandler.cs +++ b/EfEnumToLookup/LookupGenerator/IDbHandler.cs @@ -36,7 +36,8 @@ internal interface IDbHandler /// Generates the migration SQL needed to update the database to match /// the enums in the current model. /// - /// + /// Details of lookups and foreign keys to add/update + /// The generated SQL script string GenerateMigrationSql(LookupDbModel model); } } diff --git a/EfEnumToLookup/LookupGenerator/SqlServerHandler.cs b/EfEnumToLookup/LookupGenerator/SqlServerHandler.cs index 6de1668..564cfb9 100644 --- a/EfEnumToLookup/LookupGenerator/SqlServerHandler.cs +++ b/EfEnumToLookup/LookupGenerator/SqlServerHandler.cs @@ -26,12 +26,24 @@ class SqlServerHandler : IDbHandler public string TableNameSuffix { get; set; } + /// + /// Make the required changes to the database. + /// + /// Details of lookups and foreign keys to add/update + /// A callback providing a means to execute sql against the + /// server. (Or possibly write it to a file for later use. public void Apply(LookupDbModel model, Action> runSql) { var sql = BuildSql(model); runSql(sql, null); } + /// + /// Generates the migration SQL needed to update the database to match + /// the enums in the current model. + /// + /// Details of lookups and foreign keys to add/update + /// The generated SQL script public string GenerateMigrationSql(LookupDbModel model) { return BuildSql(model); From 8060db0214afe9a8d9c7d0fa697b753af5922bf2 Mon Sep 17 00:00:00 2001 From: Tim Abell Date: Tue, 18 Aug 2015 08:28:44 +0100 Subject: [PATCH 5/8] removed unused 'using's --- EfEnumToLookup/LookupGenerator/EnumToLookup.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/EfEnumToLookup/LookupGenerator/EnumToLookup.cs b/EfEnumToLookup/LookupGenerator/EnumToLookup.cs index f8138ef..889a570 100644 --- a/EfEnumToLookup/LookupGenerator/EnumToLookup.cs +++ b/EfEnumToLookup/LookupGenerator/EnumToLookup.cs @@ -2,14 +2,12 @@ { using System; using System.Collections.Generic; - using System.ComponentModel; using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Data.SqlClient; using System.Linq; using System.Reflection; using System.Text; - using System.Text.RegularExpressions; /// /// Makes up for a missing feature in Entity Framework 6.1 From 29246b5921cd34cc07c053141f7a4caba5a722e8 Mon Sep 17 00:00:00 2001 From: Tim Abell Date: Tue, 18 Aug 2015 08:53:48 +0100 Subject: [PATCH 6/8] tests - don't drop non-existent db in setup contribution from PR #33 - Svein Helge extended the contribution to cover both test classes --- EfEnumToLookupTests/Tests/TestConfiguration.cs | 5 ++++- EfEnumToLookupTests/Tests/TestStuff.cs | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/EfEnumToLookupTests/Tests/TestConfiguration.cs b/EfEnumToLookupTests/Tests/TestConfiguration.cs index 4d4b17e..4a9aeaa 100644 --- a/EfEnumToLookupTests/Tests/TestConfiguration.cs +++ b/EfEnumToLookupTests/Tests/TestConfiguration.cs @@ -16,7 +16,10 @@ public void SetUp() // Using setup rather than teardown to make it easier to inspect the database after running a test. using (var context = new MagicContext()) { - context.Database.Delete(); + if (context.Database.Exists()) + { + context.Database.Delete(); + } } } diff --git a/EfEnumToLookupTests/Tests/TestStuff.cs b/EfEnumToLookupTests/Tests/TestStuff.cs index d8f10b0..b6393b7 100644 --- a/EfEnumToLookupTests/Tests/TestStuff.cs +++ b/EfEnumToLookupTests/Tests/TestStuff.cs @@ -19,7 +19,10 @@ public void SetUp() // Using setup rather than teardown to make it easier to inspect the database after running a test. using (var context = new MagicContext()) { - context.Database.Delete(); + if (context.Database.Exists()) + { + context.Database.Delete(); + } } Database.SetInitializer(new TestInitializer(new EnumToLookup())); From bcba40ffab8415b5844a40cbf1b442488a63664f Mon Sep 17 00:00:00 2001 From: Tim Abell Date: Tue, 18 Aug 2015 08:50:21 +0100 Subject: [PATCH 7/8] make transaction optional backwards-compatible config addition fixes #32 --- EfEnumToLookup/LookupGenerator/EnumToLookup.cs | 7 +++++++ EfEnumToLookup/LookupGenerator/IDbHandler.cs | 5 +++++ .../LookupGenerator/IEnumToLookup.cs | 5 +++++ .../LookupGenerator/SqlServerHandler.cs | 18 +++++++++++++++--- 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/EfEnumToLookup/LookupGenerator/EnumToLookup.cs b/EfEnumToLookup/LookupGenerator/EnumToLookup.cs index 889a570..794c933 100644 --- a/EfEnumToLookup/LookupGenerator/EnumToLookup.cs +++ b/EfEnumToLookup/LookupGenerator/EnumToLookup.cs @@ -31,6 +31,7 @@ public EnumToLookup() NameFieldLength = 255; TableNamePrefix = "Enum_"; _enumParser = new EnumParser { SplitWords = true }; + UseTransaction = true; } /// @@ -63,6 +64,11 @@ public bool SplitWords /// public string TableNameSuffix { get; set; } + /// + /// Whether to run the changes inside a database transaction. + /// + public bool UseTransaction { get; set; } + /// /// Create any missing lookup tables, /// enforce values in the lookup tables @@ -110,6 +116,7 @@ private IDbHandler GetDbHandler() NameFieldLength = NameFieldLength, TableNamePrefix = TableNamePrefix, TableNameSuffix = TableNameSuffix, + UseTransaction = UseTransaction, }; return dbHandler; } diff --git a/EfEnumToLookup/LookupGenerator/IDbHandler.cs b/EfEnumToLookup/LookupGenerator/IDbHandler.cs index 3bbc790..b404f5b 100644 --- a/EfEnumToLookup/LookupGenerator/IDbHandler.cs +++ b/EfEnumToLookup/LookupGenerator/IDbHandler.cs @@ -24,6 +24,11 @@ internal interface IDbHandler /// string TableNameSuffix { get; set; } + /// + /// Whether to run the changes inside a database transaction. + /// + bool UseTransaction { get; set; } + /// /// Make the required changes to the database. /// diff --git a/EfEnumToLookup/LookupGenerator/IEnumToLookup.cs b/EfEnumToLookup/LookupGenerator/IEnumToLookup.cs index 0ae1c9c..7ff7700 100644 --- a/EfEnumToLookup/LookupGenerator/IEnumToLookup.cs +++ b/EfEnumToLookup/LookupGenerator/IEnumToLookup.cs @@ -34,5 +34,10 @@ public interface IEnumToLookup /// Defaults to "Enum_" set to null or "" to not have any prefix. /// string TableNamePrefix { get; set; } + + /// + /// Whether to run the changes inside a database transaction. + /// + bool UseTransaction { get; set; } } } diff --git a/EfEnumToLookup/LookupGenerator/SqlServerHandler.cs b/EfEnumToLookup/LookupGenerator/SqlServerHandler.cs index 564cfb9..d5cd5e3 100644 --- a/EfEnumToLookup/LookupGenerator/SqlServerHandler.cs +++ b/EfEnumToLookup/LookupGenerator/SqlServerHandler.cs @@ -26,6 +26,12 @@ class SqlServerHandler : IDbHandler public string TableNameSuffix { get; set; } + /// + /// Whether to run the changes inside a database transaction. + /// + public bool UseTransaction { get; set; } + + /// /// Make the required changes to the database. /// @@ -53,12 +59,18 @@ private string BuildSql(LookupDbModel model) { var sql = new StringBuilder(); sql.AppendLine("set nocount on;"); - sql.AppendLine("set xact_abort on; -- rollback on error"); - sql.AppendLine("begin tran;"); + if (UseTransaction) + { + sql.AppendLine("set xact_abort on; -- rollback on error"); + sql.AppendLine("begin tran;"); + } sql.AppendLine(CreateTables(model.Lookups)); sql.AppendLine(PopulateLookups(model.Lookups)); sql.AppendLine(AddForeignKeys(model.References)); - sql.AppendLine("commit;"); + if (UseTransaction) + { + sql.AppendLine("commit;"); + } return sql.ToString(); } From 820e1c167293d6b9ae51733f4d3f72a8706e1967 Mon Sep 17 00:00:00 2001 From: Tim Abell Date: Tue, 18 Aug 2015 08:59:10 +0100 Subject: [PATCH 8/8] version bump v1.9.0 --- ef-enum-to-lookup.nuspec | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ef-enum-to-lookup.nuspec b/ef-enum-to-lookup.nuspec index 702b9d4..3f8f8d5 100644 --- a/ef-enum-to-lookup.nuspec +++ b/ef-enum-to-lookup.nuspec @@ -2,7 +2,7 @@ ef-enum-to-lookup - 1.8.0 + 1.9.0 Enum Lookup-Table Generator for Entity Framework 6 Tim Abell http://opensource.org/licenses/MIT @@ -25,9 +25,10 @@ Creates lookup tables and foreign key constraints based on the enums used in your model. - v1.8.0 - * stable sql output ordering https://github.com/timabell/ef-enum-to-lookup/issues/29 - * stable primary key names https://github.com/timabell/ef-enum-to-lookup/issues/31 + v1.9.0 + * transaction now optional https://github.com/timabell/ef-enum-to-lookup/issues/32 + - new property "UseTransaction" to toggle, defaults to true as per old behaviour. + * code/test cleanup that shouldn't affect anything visible Tim Abell en-GB