From d392f269ba7ae43dc4907dd512effa047216e3ef Mon Sep 17 00:00:00 2001 From: mor0146 <vojtech.moravec.st@vsb.cz> Date: Tue, 9 Oct 2018 10:04:15 +0200 Subject: [PATCH] Fixed condition deciding, whether migrations should be applied. --- DataAccessTier/App.config | 10 ---------- DataAccessTier/MiddlewareContext.cs | 4 +++- ...eIfNotExistOrMigrateDatabaseInitializer.cs | 13 +++++++++++-- .../Properties/Settings.Designer.cs | 12 ------------ DataAccessTier/Properties/Settings.settings | 3 --- .../UnitOfWork/DatabaseUnitOfWork.cs | 4 ++-- MiddlewareTests/MiddlewareTests.csproj | 1 + MiddlewareTests/UnitTest1.cs | 19 +++++++++++++++---- 8 files changed, 32 insertions(+), 34 deletions(-) diff --git a/DataAccessTier/App.config b/DataAccessTier/App.config index ce2fb39..0e3baf2 100644 --- a/DataAccessTier/App.config +++ b/DataAccessTier/App.config @@ -10,9 +10,6 @@ <section name="DataAccessLayer.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </sectionGroup> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> - <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > - <section name="HaaSMiddleware.DataAccessTier.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" /> - </sectionGroup> </configSections> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> @@ -40,11 +37,4 @@ </setting> </DataAccessLayer.Properties.Settings> </applicationSettings> - <userSettings> - <HaaSMiddleware.DataAccessTier.Properties.Settings> - <setting name="CustomMiddlewareContext" serializeAs="String"> - <value>Data Source=DELLG5;Database=HaaS_Middleware_1;integrated security=True;</value> - </setting> - </HaaSMiddleware.DataAccessTier.Properties.Settings> - </userSettings> </configuration> \ No newline at end of file diff --git a/DataAccessTier/MiddlewareContext.cs b/DataAccessTier/MiddlewareContext.cs index 25c394d..78d0d84 100644 --- a/DataAccessTier/MiddlewareContext.cs +++ b/DataAccessTier/MiddlewareContext.cs @@ -19,7 +19,9 @@ namespace HaaSMiddleware.DataAccessTier internal class MiddlewareContext : DbContext { - public MiddlewareContext() : base(Properties.Settings.Default.CustomMiddlewareContext) + internal static string EFConnectionString = Properties.Settings.Default.MiddlewareContext; + + public MiddlewareContext() : base(EFConnectionString) { //"name=MiddlewareContext") { diff --git a/DataAccessTier/Migrations/CreateIfNotExistOrMigrateDatabaseInitializer.cs b/DataAccessTier/Migrations/CreateIfNotExistOrMigrateDatabaseInitializer.cs index 37ed080..ec5fc3b 100644 --- a/DataAccessTier/Migrations/CreateIfNotExistOrMigrateDatabaseInitializer.cs +++ b/DataAccessTier/Migrations/CreateIfNotExistOrMigrateDatabaseInitializer.cs @@ -1,5 +1,6 @@ using System.Data.Entity; using System.Data.Entity.Migrations; +using System.Linq; namespace HaaSMiddleware.DataAccessTier.Migrations { @@ -18,16 +19,24 @@ namespace HaaSMiddleware.DataAccessTier.Migrations bool createDatabase = !context.Database.Exists(); var migrator = new DbMigrator(_migrationConfiguration); + var pending = migrator.GetPendingMigrations(); + + if (!context.Database.CompatibleWithModel(false)) + { + System.Console.WriteLine("Migrating database:\t" + context.Database.Connection.ConnectionString); + migrator.Update(); + } - //if (migrator.GetPendingMigrations().Any()) - migrator.Update(); base.InitializeDatabase(context); if (createDatabase) { + System.Console.WriteLine("Creating database:\t" + context.Database.Connection.ConnectionString); Seed(context); + System.Console.WriteLine("Seeding database:\t" + context.Database.Connection.ConnectionString); context.SaveChanges(); + System.Console.WriteLine("Saving changes for database:\t" + context.Database.Connection.ConnectionString); } } } diff --git a/DataAccessTier/Properties/Settings.Designer.cs b/DataAccessTier/Properties/Settings.Designer.cs index 47469f2..e8414cf 100644 --- a/DataAccessTier/Properties/Settings.Designer.cs +++ b/DataAccessTier/Properties/Settings.Designer.cs @@ -41,17 +41,5 @@ namespace HaaSMiddleware.DataAccessTier.Properties { return ((string)(this["MiddlewareContext"])); } } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("Data Source=DELLG5;Database=HaaS_Middleware_1;integrated security=True;")] - public string CustomMiddlewareContext { - get { - return ((string)(this["CustomMiddlewareContext"])); - } - set { - this["CustomMiddlewareContext"] = value; - } - } } } diff --git a/DataAccessTier/Properties/Settings.settings b/DataAccessTier/Properties/Settings.settings index fcc42ca..76b76ad 100644 --- a/DataAccessTier/Properties/Settings.settings +++ b/DataAccessTier/Properties/Settings.settings @@ -13,8 +13,5 @@ </SerializableConnectionString></DesignTimeValue> <Value Profile="(Default)">Data Source=DELLG5;Database=HaaS_Middleware_1;integrated security=True;</Value> </Setting> - <Setting Name="CustomMiddlewareContext" Type="System.String" Scope="User"> - <Value Profile="(Default)">Data Source=DELLG5;Database=HaaS_Middleware_1;integrated security=True;</Value> - </Setting> </Settings> </SettingsFile> \ No newline at end of file diff --git a/DataAccessTier/UnitOfWork/DatabaseUnitOfWork.cs b/DataAccessTier/UnitOfWork/DatabaseUnitOfWork.cs index dee8357..b0270c2 100644 --- a/DataAccessTier/UnitOfWork/DatabaseUnitOfWork.cs +++ b/DataAccessTier/UnitOfWork/DatabaseUnitOfWork.cs @@ -21,8 +21,8 @@ namespace HaaSMiddleware.DataAccessTier.UnitOfWork { public DatabaseUnitOfWork(string connString) { - Properties.Settings.Default.CustomMiddlewareContext = connString; - //Properties.Settings.Default.Save(); + MiddlewareContext.EFConnectionString = connString; + this.context = new MiddlewareContext(); if (!Enum.TryParse(ConfigurationManager.AppSettings["DatabaseRepositoryType"], out this.repositoryType)) this.repositoryType = RepositoryType.LinqToEntities; diff --git a/MiddlewareTests/MiddlewareTests.csproj b/MiddlewareTests/MiddlewareTests.csproj index 5c27529..df9e38f 100644 --- a/MiddlewareTests/MiddlewareTests.csproj +++ b/MiddlewareTests/MiddlewareTests.csproj @@ -56,6 +56,7 @@ <Reference Include="System" /> <Reference Include="System.ComponentModel.DataAnnotations" /> <Reference Include="System.Core" /> + <Reference Include="System.Data" /> </ItemGroup> <ItemGroup> <Compile Include="UnitTest1.cs" /> diff --git a/MiddlewareTests/UnitTest1.cs b/MiddlewareTests/UnitTest1.cs index 4bcd15a..ff026d3 100644 --- a/MiddlewareTests/UnitTest1.cs +++ b/MiddlewareTests/UnitTest1.cs @@ -1,4 +1,5 @@ using System; +using System.Data.SqlClient; using HaaSMiddleware.DataAccessTier.UnitOfWork; using HaaSMiddleware.DomainObjects.ClusterInformation; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -11,13 +12,23 @@ namespace MiddlewareTests [TestMethod] public static void Main () { - TestCustomDb("Data Source=DELLG5;Database=HaaS_Middleware_1;integrated security=True;"); - TestCustomDb("Data Source=DELLG5;Database=HaaS_Middleware_2;integrated security=True;"); + TestCustomDb("DELLG5","HaaS_Middleware_1"); + TestCustomDb("DELLG5","HaaS_Middleware_2" + + ""); } - private static void TestCustomDb(string connString) + private static void TestCustomDb(string dataSource, string database) { - DatabaseUnitOfWork uof = new DatabaseUnitOfWork(connString); + SqlConnectionStringBuilder sqlConnStringBuilder = new SqlConnectionStringBuilder + { + DataSource = dataSource, + InitialCatalog = database, + IntegratedSecurity = true + }; + + string connStr = sqlConnStringBuilder.ConnectionString; + Console.WriteLine("Connection: " + connStr); + DatabaseUnitOfWork uof = new DatabaseUnitOfWork(connStr); foreach (Cluster c in uof.ClusterRepository.GetAll()) { Console.WriteLine(c); -- GitLab