Commit d392f269 authored by Vojtech Moravec's avatar Vojtech Moravec
Browse files

Fixed condition deciding, whether migrations should be applied.

parent 7650a5fb
......@@ -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
......@@ -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") {
......
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);
}
}
}
......
......@@ -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;
}
}
}
}
......@@ -13,8 +13,5 @@
&lt;/SerializableConnectionString&gt;</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
......@@ -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;
......
......@@ -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" />
......
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);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment