diff --git a/EfEnumToLookup/LookupGenerator/EnumToLookup.cs b/EfEnumToLookup/LookupGenerator/EnumToLookup.cs index f8138ef..794c933 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 @@ -33,6 +31,7 @@ public EnumToLookup() NameFieldLength = 255; TableNamePrefix = "Enum_"; _enumParser = new EnumParser { SplitWords = true }; + UseTransaction = true; } /// @@ -65,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 @@ -112,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 c26c47c..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. /// @@ -36,7 +41,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/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 a3254b7..12b5289 100644 --- a/EfEnumToLookup/LookupGenerator/SqlServerHandler.cs +++ b/EfEnumToLookup/LookupGenerator/SqlServerHandler.cs @@ -9,31 +9,47 @@ 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; } + /// + /// Whether to run the changes inside a database transaction. + /// + public bool UseTransaction { 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); @@ -43,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(); } 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 3bbe946..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())); @@ -63,9 +66,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); 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()) {