Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,276 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.Migrations
{
using System.Data.Entity.Spatial;
using System.Linq;
using Xunit;
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlServerCe, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.VB)]
public class AddColumnScenarios : DbTestCase
{
private class AddColumnMigration : DbMigration
{
public override void Up()
{
AddColumn("MigrationsCustomers", "new_col", c => c.Int(nullable: false, defaultValue: 0));
}
}
[MigrationsTheory]
public void Can_add_non_nullable_column_when_no_data_present()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1>(new AddColumnMigration());
migrator.Update();
var column = Info.Columns.SingleOrDefault(c => c.TableName == "MigrationsCustomers" && c.Name == "new_col");
Assert.NotNull(column);
Assert.Equal("NO", column.IsNullable);
Assert.True(column.Default.Contains("0"));
}
[MigrationsTheory]
public void Can_add_non_nullable_column_with_default_value_when_data_present()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
using (var context = CreateContext<ShopContext_v1>())
{
context.Customers.Add(
new MigrationsCustomer
{
HomeAddress = new MigrationsAddress(),
WorkAddress = new MigrationsAddress(),
DateOfBirth = DateTime.Now
});
context.SaveChanges();
}
migrator = CreateMigrator<ShopContext_v1>(new AddColumnMigration());
migrator.Update();
Assert.True(ColumnExists("MigrationsCustomers", "new_col"));
}
private class AddColumnWithCustomSqlMigration : DbMigration
{
public override void Up()
{
AddColumn("MigrationsCustomers", "new_col", c => c.Int(nullable: false, defaultValue: 0));
Sql("select new_col from MigrationsCustomers");
}
}
[MigrationsTheory]
public void Can_add_column_and_then_reference_it_in_custom_sql()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1>(new AddColumnWithCustomSqlMigration());
migrator.Update();
}
private class AddColumnWithDateTimeDefault : DbMigration
{
public override void Up()
{
AddColumn("MigrationsCustomers", "date_col", c => c.DateTime(nullable: false, defaultValue: DateTime.Today));
}
}
[MigrationsTheory]
public void Can_add_column_with_datetime_default()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1>(new AddColumnWithDateTimeDefault());
migrator.Update();
}
private class AddColumnWithGeographyDefault : DbMigration
{
public override void Up()
{
AddColumn(
"MigrationsCustomers", "we_know_where_you_live",
c => c.Geography(nullable: false, defaultValue: DbGeography.FromText("POINT (6 7)")));
}
}
[MigrationsTheory]
public void Can_add_column_with_geography_default()
{
WhenNotSqlCe(
() =>
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1>(new AddColumnWithGeographyDefault());
migrator.Update();
});
}
private class AddColumnWithGeometryDefault : DbMigration
{
public override void Up()
{
AddColumn(
"MigrationsCustomers", "head_shape",
c => c.Geometry(nullable: false, defaultValue: DbGeometry.FromText("POINT (6 7)")));
}
}
[MigrationsTheory]
public void Can_add_column_with_geometry_default()
{
WhenNotSqlCe(
() =>
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1>(new AddColumnWithGeometryDefault());
migrator.Update();
});
}
private class AddColumnWithCustomStoreType : DbMigration
{
public override void Up()
{
AddColumn("MigrationsCustomers", "image_col", c => c.Binary(storeType: "image"));
}
}
[MigrationsTheory]
public void Can_add_column_with_custom_store_type()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1>(new AddColumnWithCustomStoreType());
migrator.Update();
var column = Info.Columns.SingleOrDefault(c => c.TableName == "MigrationsCustomers" && c.Name == "image_col");
Assert.NotNull(column);
Assert.Equal("image", column.Type);
}
private class AddTimestampColumn : DbMigration
{
public override void Up()
{
AddColumn("MigrationsCustomers", "version", c => c.Binary(nullable: false, timestamp: true));
}
}
[MigrationsTheory]
public void Can_add_non_nullable_timestamp_column()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1>(new AddTimestampColumn());
migrator.Update();
}
private class AddNonNullableColumnsWithNoDefaults : DbMigration
{
public override void Up()
{
AddColumn("MigrationsCustomers", "Binary", c => c.Binary(nullable: false));
AddColumn("MigrationsCustomers", "Boolean", c => c.Boolean(nullable: false));
AddColumn("MigrationsCustomers", "Byte", c => c.Byte(nullable: false));
AddColumn("MigrationsCustomers", "DateTime", c => c.DateTime(nullable: false));
AddColumn("MigrationsCustomers", "DateTimeOffset", c => c.DateTimeOffset(nullable: false));
AddColumn("MigrationsCustomers", "Decimal", c => c.Decimal(nullable: false));
AddColumn("MigrationsCustomers", "Double", c => c.Double(nullable: false));
AddColumn("MigrationsCustomers", "Guid", c => c.Guid(nullable: false));
AddColumn("MigrationsCustomers", "Int", c => c.Int(nullable: false));
AddColumn("MigrationsCustomers", "Long", c => c.Long(nullable: false));
AddColumn("MigrationsCustomers", "Short", c => c.Short(nullable: false));
AddColumn("MigrationsCustomers", "Single", c => c.Single(nullable: false));
AddColumn("MigrationsCustomers", "String", c => c.String(nullable: false));
AddColumn("MigrationsCustomers", "Time", c => c.Time(nullable: false));
}
}
[MigrationsTheory]
public void Can_add_non_nullable_columns_and_valid_defaults_generated_when_existing_data_in_table()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
using (var context = CreateContext<ShopContext_v1>())
{
context.Customers.Add(
new MigrationsCustomer
{
HomeAddress = new MigrationsAddress(),
WorkAddress = new MigrationsAddress(),
DateOfBirth = DateTime.Now
});
context.SaveChanges();
}
var addNonNullableColumnsWithNoDefaults = new AddNonNullableColumnsWithNoDefaults();
WhenSqlCe(
() =>
{
addNonNullableColumnsWithNoDefaults.GetOperations().RemoveAt(13);
addNonNullableColumnsWithNoDefaults.GetOperations().RemoveAt(4);
});
migrator = CreateMigrator<ShopContext_v1>(addNonNullableColumnsWithNoDefaults);
migrator.Update();
}
}
}

View File

@@ -0,0 +1,128 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.Migrations
{
using System.Linq;
using Xunit;
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlServerCe, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.VB)]
public class AddForeignKeyScenarios : DbTestCase
{
private class AddSimpleForeignKeyMigration : DbMigration
{
public override void Up()
{
AddForeignKey("OrderLines", "OrderId", "ordering.Orders", "OrderId", name: "FK_Custom_Name");
}
}
[MigrationsTheory]
public void Can_add_simple_foreign_key_constraint()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1>(new AddSimpleForeignKeyMigration());
migrator.Update();
var foreignKey = Info.TableConstraints.OfType<ReferentialConstraintInfo>().SingleOrDefault(rc => rc.Name == "FK_Custom_Name");
Assert.NotNull(foreignKey);
Assert.Equal(1, foreignKey.KeyColumnUsages.Count());
Assert.True(
foreignKey.KeyColumnUsages.Any(
kcu => kcu.Position == 1 && kcu.ColumnTableName == "OrderLines" && kcu.ColumnName == "OrderId"));
Assert.Equal(1, foreignKey.UniqueConstraint.KeyColumnUsages.Count());
var keyColumnUsage =
foreignKey.UniqueConstraint.KeyColumnUsages.SingleOrDefault(
kcu => kcu.Position == 1 && kcu.ColumnTableName == "Orders" && kcu.ColumnName == "OrderId");
Assert.NotNull(keyColumnUsage);
WhenNotSqlCe(() => Assert.Equal("ordering", keyColumnUsage.ColumnTableSchema));
}
private class AddCompositeForeignKeyMigration : DbMigration
{
public override void Up()
{
AddForeignKey("OrderLines", new[] { "ProductId", "Sku" }, "MigrationsProducts", new[] { "ProductId", "Sku" });
}
}
[MigrationsTheory]
public void Can_add_composite_foreign_key_constraint()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1>(new AddCompositeForeignKeyMigration());
migrator.Update();
var foreignKey =
Info.TableConstraints.OfType<ReferentialConstraintInfo>().SingleOrDefault(
rc => rc.Name == "FK_OrderLines_MigrationsProducts_ProductId_Sku");
Assert.NotNull(foreignKey);
Assert.Equal(2, foreignKey.KeyColumnUsages.Count());
Assert.True(
foreignKey.KeyColumnUsages.Any(
kcu => kcu.Position == 1 && kcu.ColumnTableName == "OrderLines" && kcu.ColumnName == "ProductId"));
Assert.True(
foreignKey.KeyColumnUsages.Any(kcu => kcu.Position == 2 && kcu.ColumnTableName == "OrderLines" && kcu.ColumnName == "Sku"));
Assert.Equal(2, foreignKey.UniqueConstraint.KeyColumnUsages.Count());
Assert.True(
foreignKey.UniqueConstraint.KeyColumnUsages.Any(
kcu => kcu.Position == 1 && kcu.ColumnTableName == "MigrationsProducts" && kcu.ColumnName == "ProductId"));
Assert.True(
foreignKey.UniqueConstraint.KeyColumnUsages.Any(
kcu => kcu.Position == 2 && kcu.ColumnTableName == "MigrationsProducts" && kcu.ColumnName == "Sku"));
}
private class AddForeignKeyNoPrincipalMigration : DbMigration
{
public override void Up()
{
AddForeignKey("OrderLines", new[] { "ProductId", "Sku" }, "MigrationsProducts");
}
}
[MigrationsTheory]
public void Can_add_composite_foreign_key_constraint_when_principal_columns_not_specified()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1>(new AddForeignKeyNoPrincipalMigration());
migrator.Update();
var foreignKey =
Info.TableConstraints.OfType<ReferentialConstraintInfo>().SingleOrDefault(
rc => rc.Name == "FK_OrderLines_MigrationsProducts_ProductId_Sku");
Assert.NotNull(foreignKey);
Assert.Equal(2, foreignKey.KeyColumnUsages.Count());
Assert.True(
foreignKey.KeyColumnUsages.Any(
kcu => kcu.Position == 1 && kcu.ColumnTableName == "OrderLines" && kcu.ColumnName == "ProductId"));
Assert.True(
foreignKey.KeyColumnUsages.Any(kcu => kcu.Position == 2 && kcu.ColumnTableName == "OrderLines" && kcu.ColumnName == "Sku"));
Assert.Equal(2, foreignKey.UniqueConstraint.KeyColumnUsages.Count());
Assert.True(
foreignKey.UniqueConstraint.KeyColumnUsages.Any(
kcu => kcu.Position == 1 && kcu.ColumnTableName == "MigrationsProducts" && kcu.ColumnName == "ProductId"));
Assert.True(
foreignKey.UniqueConstraint.KeyColumnUsages.Any(
kcu => kcu.Position == 2 && kcu.ColumnTableName == "MigrationsProducts" && kcu.ColumnName == "Sku"));
}
}
}

View File

@@ -0,0 +1,39 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.Migrations
{
using System.Linq;
using Xunit;
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlServerCe, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.VB)]
public class AddPrimaryKeyScenarios : DbTestCase
{
private class AddPrimaryKeyMigration : DbMigration
{
public override void Up()
{
CreateTable(
"T", t => new
{
Id = t.Int(nullable: false)
});
AddPrimaryKey("T", "Id", name: "my_pk", clustered: false);
}
}
[MigrationsTheory]
public void Can_add_non_clustered_primary_key()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>(new AddPrimaryKeyMigration());
migrator.Update();
Assert.NotNull(Info.TableConstraints.SingleOrDefault(c => c.Name == "my_pk"));
}
}
}

View File

@@ -0,0 +1,124 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.Migrations
{
using System.Linq;
using Xunit;
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlServerCe, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.VB)]
public class AlterColumnScenarios : DbTestCase
{
private class AlterColumnWithDefault : DbMigration
{
public override void Up()
{
AlterColumn("MigrationsCustomers", "Name", c => c.String(defaultValue: "Bill"));
}
}
[MigrationsTheory]
public void Can_change_column_to_have_default_value()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1>(new AlterColumnWithDefault());
migrator.Update();
var column = Info.Columns.Single(c => c.TableName == "MigrationsCustomers" && c.Name == "Name");
Assert.True(column.Default.Contains("'Bill'"));
}
private class AlterColumnWithIdentityMigration : DbMigration
{
public override void Up()
{
AlterColumn("MigrationsCustomers", "CustomerNumber", c => c.Long(identity: true));
}
}
[MigrationsTheory]
public void Can_change_column_to_identity_column_when_no_data_present()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1>(new AlterColumnWithIdentityMigration());
migrator.Update();
}
private class AlterColumnMigration : DbMigration
{
public override void Up()
{
AlterColumn("MigrationsCustomers", "Name", c => c.String(nullable: false));
}
}
[MigrationsTheory]
public void Can_change_column_to_non_nullable_column_when_no_data_present()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1>(new AlterColumnMigration());
migrator.Update();
var column = Info.Columns.Single(c => c.TableName == "MigrationsCustomers" && c.Name == "Name");
Assert.Equal("NO", column.IsNullable);
}
private class AlterColumnWithDefaultMigration : DbMigration
{
public override void Up()
{
AlterColumn("MigrationsCustomers", "Name", c => c.String(nullable: false, defaultValue: string.Empty));
}
}
// UNDONE: Can't handle this yet (table rebuild)
// [MigrationsTheory]
public void Can_change_colum_to_non_nullable_column_with_default_value_when_data_present()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
using (var context = CreateContext<ShopContext_v1>())
{
context.Customers.Add(
new MigrationsCustomer
{
HomeAddress = new MigrationsAddress(),
WorkAddress = new MigrationsAddress(),
DateOfBirth = DateTime.Now
});
context.SaveChanges();
}
migrator = CreateMigrator<ShopContext_v1>(new AlterColumnWithDefaultMigration());
migrator.Update();
var column = Info.Columns.Single(c => c.TableName == "MigrationsCustomers" && c.Name == "Name");
Assert.Equal("NO", column.IsNullable);
Assert.True(column.Default.Contains("''"));
}
}
}

View File

@@ -0,0 +1,195 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.Migrations
{
using System.Collections.Generic;
using System.Data.Entity.Migrations.Design;
using System.Data.Entity.Migrations.Infrastructure;
using System.Data.Entity.Migrations.Model;
using System.Data.Entity.Migrations.Sql;
using System.Linq;
using Xunit;
using Xunit.Extensions;
public abstract class AutoAndGenerateTestCase<TContextV1, TContextV2> : DbTestCase
where TContextV1 : DbContext
where TContextV2 : DbContext
{
private SqlInterceptor _upVerifier;
private SqlInterceptor _downVerifier;
private ScaffoldedMigration _generatedMigration_v1;
protected bool UpDataLoss { get; set; }
protected bool DownDataLoss { get; set; }
public override void Init(DatabaseProvider provider, ProgrammingLanguage language)
{
base.Init(provider, language);
_upVerifier = new SqlInterceptor(VerifyUpOperations);
_downVerifier = new SqlInterceptor(VerifyDownOperations);
}
[MigrationsTheory]
public void Automatic()
{
ResetDatabaseToV1();
DbMigrationsConfiguration migrationsConfiguration;
try
{
migrationsConfiguration =
CreateMigrationsConfiguration<TContextV2>(scaffoldedMigrations: _generatedMigration_v1);
migrationsConfiguration.SetSqlGenerator(DbProviders.Sql, _upVerifier);
migrationsConfiguration.SetSqlGenerator(DbProviders.SqlCe, _upVerifier);
new DbMigrator(migrationsConfiguration).Update();
Assert.False(UpDataLoss);
}
catch (AutomaticDataLossException)
{
Assert.True(UpDataLoss);
migrationsConfiguration = CreateMigrationsConfiguration<TContextV2>(
automaticDataLossEnabled: true, scaffoldedMigrations: _generatedMigration_v1);
migrationsConfiguration.SetSqlGenerator(DbProviders.Sql, _upVerifier);
migrationsConfiguration.SetSqlGenerator(DbProviders.SqlCe, _upVerifier);
new DbMigrator(migrationsConfiguration).Update();
}
// NOTE: This prevents no-op migrations from silently succeeding
if (!_upVerifier.WasCalled())
{
VerifyUpOperations(Enumerable.Empty<MigrationOperation>());
}
// Bring up via automatic
CreateMigrator<TContextV2>(automaticDataLossEnabled: true, scaffoldedMigrations: _generatedMigration_v1).Update();
try
{
migrationsConfiguration =
CreateMigrationsConfiguration<TContextV2>(scaffoldedMigrations: _generatedMigration_v1);
migrationsConfiguration.SetSqlGenerator(DbProviders.Sql, _downVerifier);
migrationsConfiguration.SetSqlGenerator(DbProviders.SqlCe, _downVerifier);
new DbMigrator(migrationsConfiguration).Update(_generatedMigration_v1.MigrationId);
Assert.False(DownDataLoss);
}
catch (AutomaticDataLossException)
{
Assert.True(DownDataLoss);
migrationsConfiguration = CreateMigrationsConfiguration<TContextV2>(
automaticDataLossEnabled: true, scaffoldedMigrations: _generatedMigration_v1);
migrationsConfiguration.SetSqlGenerator(DbProviders.Sql, _downVerifier);
migrationsConfiguration.SetSqlGenerator(DbProviders.SqlCe, _downVerifier);
new DbMigrator(migrationsConfiguration).Update(_generatedMigration_v1.MigrationId);
}
if (!_downVerifier.WasCalled())
{
VerifyDownOperations(Enumerable.Empty<MigrationOperation>());
}
}
[MigrationsTheory]
[InlineData(ProgrammingLanguage.CSharp)]
[InlineData(ProgrammingLanguage.VB)]
public void Generated(ProgrammingLanguage programmingLanguage)
{
ProgrammingLanguage = programmingLanguage;
ResetDatabaseToV1();
var migrator = CreateMigrator<TContextV2>(scaffoldedMigrations: _generatedMigration_v1);
var generatedMigration_v2 = new MigrationScaffolder(migrator.Configuration).Scaffold("V2");
var migrationsConfiguration =
CreateMigrationsConfiguration<TContextV2>(
scaffoldedMigrations: new[] { _generatedMigration_v1, generatedMigration_v2 });
migrationsConfiguration.SetSqlGenerator(DbProviders.Sql, _upVerifier);
migrationsConfiguration.SetSqlGenerator(DbProviders.SqlCe, _upVerifier);
new DbMigrator(migrationsConfiguration).Update();
// Bring up via generated
CreateMigrator<TContextV2>(
scaffoldedMigrations: new[] { _generatedMigration_v1, generatedMigration_v2 })
.Update();
migrationsConfiguration =
CreateMigrationsConfiguration<TContextV2>(
scaffoldedMigrations: new[] { _generatedMigration_v1, generatedMigration_v2 });
migrationsConfiguration.SetSqlGenerator(DbProviders.Sql, _downVerifier);
migrationsConfiguration.SetSqlGenerator(DbProviders.SqlCe, _downVerifier);
new DbMigrator(migrationsConfiguration).Update(_generatedMigration_v1.MigrationId);
}
private void ResetDatabaseToV1()
{
ResetDatabase();
var migrator = CreateMigrator<TContextV1>();
_generatedMigration_v1 = new MigrationScaffolder(migrator.Configuration).Scaffold("V1");
CreateMigrator<TContextV1>(scaffoldedMigrations: _generatedMigration_v1).Update();
}
protected abstract void VerifyUpOperations(IEnumerable<MigrationOperation> migrationOperations);
protected abstract void VerifyDownOperations(IEnumerable<MigrationOperation> migrationOperations);
private class SqlInterceptor : MigrationSqlGenerator
{
private static readonly IList<Type> _excludedTypes
= new List<Type>
{
typeof(HistoryOperation)
};
private readonly Action<IEnumerable<MigrationOperation>> _verifyAction;
private bool _wasCalled;
public SqlInterceptor(Action<IEnumerable<MigrationOperation>> verifyAction)
{
_verifyAction = verifyAction;
}
public override IEnumerable<MigrationStatement> Generate(
IEnumerable<MigrationOperation> migrationOperations, string providerManifestToken)
{
_wasCalled = true;
_verifyAction(migrationOperations.Where(mo => !_excludedTypes.Contains(mo.GetType())).ToList());
return Enumerable.Empty<MigrationStatement>();
}
public bool WasCalled()
{
var wasCalled = _wasCalled;
_wasCalled = false;
return wasCalled;
}
}
}
#region Model stubs
public class AutoAndGenerateContext_v1 : DbContext
{
public AutoAndGenerateContext_v1()
: base("AutoAndGenerateContext_v1")
{
}
}
public class AutoAndGenerateContext_v2 : DbContext
{
public AutoAndGenerateContext_v2()
: base("AutoAndGenerateContext_v2")
{
}
}
#endregion
}

View File

@@ -0,0 +1,86 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.Migrations
{
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlServerCe, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.VB)]
public class CreateIndexScenarios : DbTestCase
{
private class CreateSimpleIndexMigration : DbMigration
{
public override void Up()
{
CreateIndex("OrderLines", "OrderId", unique: true, name: "IX_Custom_Name");
}
}
[MigrationsTheory]
public void Can_create_simple_index()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1>(new CreateSimpleIndexMigration());
migrator.Update();
}
private class CreateCompositeIndexMigration : DbMigration
{
public override void Up()
{
CreateIndex("OrderLines", new[] { "ProductId", "Sku" }, true);
}
}
[MigrationsTheory]
public void Can_create_composite_index()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1, CreateCompositeIndexMigration>();
migrator.Update();
}
[MigrationsTheory]
public void Bug_49966_should_not_generate_duplicate_foreign_keys()
{
ResetDatabase();
var migrator = CreateMigrator<ProcessedTransactionContext>();
migrator.Update();
}
private class CreateClusteredIndexMigration : DbMigration
{
public override void Up()
{
CreateIndex("OrderLines", "OrderId", name: "IX_Custom_Name", clustered: true);
}
}
[MigrationsTheory]
public void Can_create_clustered_index()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1>(new CreateClusteredIndexMigration());
migrator.Update();
}
}
}

View File

@@ -0,0 +1,151 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.Migrations
{
using System.Data.Entity.Migrations.Infrastructure;
using System.Linq;
using Xunit;
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlServerCe, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.VB)]
public class CreateTableScenarios : DbTestCase
{
private class CreateOobTableFkMigration : DbMigration
{
public override void Up()
{
CreateTable(
"Oob_Principal", t => new
{
Id = t.Int()
})
.PrimaryKey(t => t.Id);
CreateTable(
"Oob_Dependent", t => new
{
Id = t.Int(),
Fk = t.Int()
})
.ForeignKey("Oob_Principal", t => t.Fk);
}
}
[MigrationsTheory]
public void Can_create_oob_table_with_inline_fk()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1>(new CreateOobTableFkMigration());
migrator.Update();
var principalTable = Info.Tables.SingleOrDefault(t => t.Name == "Oob_Principal");
Assert.NotNull(principalTable);
Assert.Equal(1, principalTable.Columns.Count());
Assert.True(principalTable.Columns.Any(c => c.Name == "Id" && c.Type == "int"));
var principalPrimaryKey = principalTable.Constraints.OfType<PrimaryKeyConstraintInfo>().SingleOrDefault();
Assert.NotNull(principalPrimaryKey);
Assert.Equal(1, principalPrimaryKey.KeyColumnUsages.Count());
Assert.True(principalPrimaryKey.KeyColumnUsages.Any(kcu => kcu.ColumnName == "Id"));
var dependentTable = Info.Tables.SingleOrDefault(t => t.Name == "Oob_Dependent");
Assert.Equal(2, dependentTable.Columns.Count());
Assert.True(dependentTable.Columns.Any(c => c.Name == "Id" && c.Type == "int"));
Assert.True(dependentTable.Columns.Any(c => c.Name == "Fk" && c.Type == "int"));
var foreignKey = dependentTable.Constraints.OfType<ReferentialConstraintInfo>().SingleOrDefault();
Assert.NotNull(foreignKey);
Assert.Equal(1, foreignKey.KeyColumnUsages.Count());
Assert.True(foreignKey.KeyColumnUsages.Any(kcu => kcu.ColumnName == "Fk"));
Assert.Equal(1, foreignKey.UniqueConstraint.KeyColumnUsages.Count());
Assert.True(
foreignKey.UniqueConstraint.KeyColumnUsages.Any(kcu => kcu.ColumnTableName == "Oob_Principal" && kcu.ColumnName == "Id"));
}
private class CreateOobTableInvalidFkMigration : DbMigration
{
public override void Up()
{
CreateTable(
"Oob_Dependent", t => new
{
Id = t.Int(),
Fk = t.Int()
})
.ForeignKey("Oob_Principal", t => t.Fk);
}
}
[MigrationsTheory]
public void Throws_on_create_oob_table_with_invalid_fk()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1>(new CreateOobTableInvalidFkMigration());
Assert.Throws<MigrationsException>(() => migrator.Update())
.ValidateMessage("PartialFkOperation", "Oob_Dependent", "Fk");
}
private class CreateCustomColumnNameMigration : DbMigration
{
public override void Up()
{
CreateTable(
"Foo", t => new
{
Id = t.Int(name: "12 Foo Id")
});
}
}
[MigrationsTheory]
public void Can_create_table_with_custom_column_name()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1>(new CreateCustomColumnNameMigration());
migrator.Update();
Assert.True(ColumnExists("Foo", "12 Foo Id"));
}
private class CreateCustomClusteredIndex : DbMigration
{
public override void Up()
{
CreateTable(
"Foo", t => new
{
Id = t.Int(nullable: false),
Ix = t.Int()
})
.PrimaryKey(t => t.Id, clustered: false)
.Index(t => t.Ix, clustered: true);
}
}
[MigrationsTheory]
public void Can_create_table_with_custom_clustered_index()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>(new CreateCustomClusteredIndex());
migrator.Update();
}
}
}

View File

@@ -0,0 +1,102 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.Migrations
{
using System.Data.Entity.Migrations.Design;
using System.Data.Entity.ModelConfiguration.Conventions;
using Xunit;
public class CrossDatabaseScenarios : DbTestCase
{
private class CrossProviderContext_v1 : ShopContext_v1
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<SqlCePropertyMaxLengthConvention>();
base.OnModelCreating(modelBuilder);
}
}
private class CrossProviderContext_v2 : CrossProviderContext_v1
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<MigrationsCustomer>().Ignore(c => c.CustomerNumber);
}
}
[MigrationsTheory]
public void Can_scaffold_on_sql_server_and_run_on_ce()
{
DatabaseProvider = DatabaseProvider.SqlClient;
ResetDatabase();
var migrator = CreateMigrator<CrossProviderContext_v1>();
var scaffoldedMigration = new MigrationScaffolder(migrator.Configuration).Scaffold("Migration");
DatabaseProvider = DatabaseProvider.SqlServerCe;
ResetDatabase();
migrator = CreateMigrator<CrossProviderContext_v1>(scaffoldedMigrations: scaffoldedMigration);
migrator.Update();
Assert.True(TableExists("MigrationsProducts"));
}
[MigrationsTheory]
public void Can_scaffold_on_ce_and_run_on_sql()
{
DatabaseProvider = DatabaseProvider.SqlServerCe;
ResetDatabase();
var migrator = CreateMigrator<CrossProviderContext_v1>();
var scaffoldedMigration = new MigrationScaffolder(migrator.Configuration).Scaffold("Migration");
DatabaseProvider = DatabaseProvider.SqlClient;
ResetDatabase();
migrator = CreateMigrator<CrossProviderContext_v1>(scaffoldedMigrations: scaffoldedMigration);
migrator.Update();
Assert.True(TableExists("MigrationsProducts"));
}
[MigrationsTheory]
public void Can_scaffold_on_sql_and_run_on_ce_after_initial_auto()
{
DatabaseProvider = DatabaseProvider.SqlClient;
ResetDatabase();
var migrator = CreateMigrator<CrossProviderContext_v1>();
migrator.Update();
migrator = CreateMigrator<CrossProviderContext_v2>();
var scaffoldedMigration = new MigrationScaffolder(migrator.Configuration).Scaffold("Migration");
DatabaseProvider = DatabaseProvider.SqlServerCe;
ResetDatabase();
migrator = CreateMigrator<CrossProviderContext_v2>(scaffoldedMigrations: scaffoldedMigration);
migrator.Update();
Assert.True(TableExists("MigrationsProducts"));
Assert.False(ColumnExists("MigrationsProducts", "CustomerNumber"));
}
}
}

View File

@@ -0,0 +1,36 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.Migrations
{
using Xunit;
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlServerCe, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.VB)]
public class CustomSqlScenarios : DbTestCase
{
private class CustomSqlMigration : DbMigration
{
public override void Up()
{
Sql("CREATE TABLE [Foo](Id [int])");
}
}
[MigrationsTheory]
public void Can_update_when_migration_contains_custom_sql()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1>(new CustomSqlMigration());
migrator.Update();
Assert.True(TableExists("Foo"));
}
}
}

View File

@@ -0,0 +1,305 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.Migrations
{
using System.Data.Entity.Migrations.Design;
using System.Data.Entity.Migrations.Infrastructure;
using Xunit;
using Xunit.Extensions;
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlServerCe, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.VB)]
public class DashScriptScenarios : DbTestCase
{
private string CreateMetadataStatement
{
get
{
if (DatabaseProvider == DatabaseProvider.SqlServerCe)
{
return "CREATE TABLE [__MigrationHistory]";
}
return "CREATE TABLE [dbo].[__MigrationHistory]";
}
}
private string DropMetadataStatement
{
get
{
if (DatabaseProvider == DatabaseProvider.SqlServerCe)
{
return "DROP TABLE [__MigrationHistory]";
}
return "DROP TABLE [dbo].[__MigrationHistory]";
}
}
[MigrationsTheory]
public void Can_script_pending_migrations()
{
ResetDatabase();
var migrator1 = CreateMigrator<ShopContext_v1>();
var version1 = new MigrationScaffolder(migrator1.Configuration).Scaffold("Version1");
CreateMigrator<ShopContext_v1>(scaffoldedMigrations: version1).Update();
var migrator2 = CreateMigrator<ShopContext_v2>(scaffoldedMigrations: version1);
var version2 = new MigrationScaffolder(migrator2.Configuration).Scaffold("Version2");
var migrator3 = CreateMigrator<ShopContext_v2>(scaffoldedMigrations: new[] { version1, version2 });
var scriptingDecorator = new MigratorScriptingDecorator(migrator3);
var script = scriptingDecorator.ScriptUpdate(null, null);
Assert.False(script.Contains("Version1"));
Assert.True(script.Contains("Version2"));
Assert.False(script.Contains("AutomaticMigration"));
}
[MigrationsTheory]
[InlineData(true)]
[InlineData(false)]
public void Can_script_windows(bool whenDatabaseExists)
{
ResetDatabase();
var migrator1 = CreateMigrator<ShopContext_v1>();
var version1 = new MigrationScaffolder(migrator1.Configuration).Scaffold("Version1");
CreateMigrator<ShopContext_v1>(scaffoldedMigrations: version1).Update();
var migrator2 = CreateMigrator<ShopContext_v2>(scaffoldedMigrations: version1);
var version2 = new MigrationScaffolder(migrator2.Configuration).Scaffold("Version2");
CreateMigrator<ShopContext_v2>(scaffoldedMigrations: new[] { version1, version2 }).Update();
var migrator3 = CreateMigrator<ShopContext_v3>(scaffoldedMigrations: new[] { version1, version2 });
var version3 = new MigrationScaffolder(migrator3.Configuration).Scaffold("Version3");
var migrator4 = CreateMigrator<ShopContext_v3>(scaffoldedMigrations: new[] { version1, version2, version3 });
var scriptingDecorator = new MigratorScriptingDecorator(migrator4);
if (!whenDatabaseExists)
{
ResetDatabase();
}
// All
var script = scriptingDecorator.ScriptUpdate(DbMigrator.InitialDatabase, null);
Assert.True(script.Contains(CreateMetadataStatement));
Assert.True(script.Contains("Version1"));
Assert.True(script.Contains("Version2"));
Assert.True(script.Contains("Version3"));
Assert.False(script.Contains("AutomaticMigration"));
// 1
script = scriptingDecorator.ScriptUpdate(DbMigrator.InitialDatabase, version1.MigrationId);
Assert.True(script.Contains(CreateMetadataStatement));
Assert.True(script.Contains("Version1"));
Assert.False(script.Contains("Version2"));
Assert.False(script.Contains("Version3"));
Assert.False(script.Contains("AutomaticMigration"));
// 1 & 2
script = scriptingDecorator.ScriptUpdate(DbMigrator.InitialDatabase, version2.MigrationId);
Assert.True(script.Contains(CreateMetadataStatement));
Assert.True(script.Contains("Version1"));
Assert.True(script.Contains("Version2"));
Assert.False(script.Contains("Version3"));
Assert.False(script.Contains("AutomaticMigration"));
}
[MigrationsTheory]
[InlineData(true)]
[InlineData(false)]
public void Can_script_first_migration_with_leading_automatic_migration(bool whenDatabaseExists)
{
ResetDatabase();
CreateMigrator<ShopContext_v1>().Update();
var migrator1 = CreateMigrator<ShopContext_v2>();
var version2 = new MigrationScaffolder(migrator1.Configuration).Scaffold("Version2");
var migrator2 = CreateMigrator<ShopContext_v2>(scaffoldedMigrations: version2);
migrator2.Update();
if (!whenDatabaseExists)
{
ResetDatabase();
}
var scriptingDecorator
= new MigratorScriptingDecorator(CreateMigrator<ShopContext_v2>(scaffoldedMigrations: version2));
// Act
var script = scriptingDecorator.ScriptUpdate(DbMigrator.InitialDatabase, version2.MigrationId);
// Assert
Assert.True(script.Contains(CreateMetadataStatement));
Assert.True(script.Contains("AutomaticMigration"));
Assert.True(script.Contains("Version2"));
}
[MigrationsTheory]
[InlineData(true)]
[InlineData(false)]
public void Can_script_middle_migration_with_leading_automatic_migration(bool whenDatabaseExists)
{
ResetDatabase();
var migrator1 = CreateMigrator<ShopContext_v1>();
var version1 = new MigrationScaffolder(migrator1.Configuration).Scaffold("Version1");
CreateMigrator<ShopContext_v1>(scaffoldedMigrations: version1).Update();
CreateMigrator<ShopContext_v2>(automaticDataLossEnabled: true, scaffoldedMigrations: version1).Update();
var migrator2 = CreateMigrator<ShopContext_v3>(scaffoldedMigrations: version1);
var version3 = new MigrationScaffolder(migrator2.Configuration).Scaffold("Version3");
var migrator3 = CreateMigrator<ShopContext_v3>(
automaticDataLossEnabled: true, scaffoldedMigrations: new[] { version1, version3 });
migrator3.Update();
var scriptingDecorator = new MigratorScriptingDecorator(migrator3);
if (!whenDatabaseExists)
{
ResetDatabase();
}
// Act
var script = scriptingDecorator.ScriptUpdate(version1.MigrationId, version3.MigrationId);
// Assert
Assert.False(script.Contains(CreateMetadataStatement));
Assert.False(script.Contains("Version1"));
Assert.True(script.Contains("AutomaticMigration"));
Assert.True(script.Contains("Version3"));
}
[MigrationsTheory]
[InlineData(true)]
[InlineData(false)]
public void Can_script_last_migration_with_trailing_automatic_migration(bool whenDatabaseExists)
{
ResetDatabase();
var migrator1 = CreateMigrator<ShopContext_v1>();
var version1 = new MigrationScaffolder(migrator1.Configuration).Scaffold("Version1");
var migrator2 = CreateMigrator<ShopContext_v2>(automaticDataLossEnabled: true, scaffoldedMigrations: version1);
migrator2.Update();
var scriptingDecorator = new MigratorScriptingDecorator(migrator2);
if (!whenDatabaseExists)
{
ResetDatabase();
}
// Act
var script = scriptingDecorator.ScriptUpdate(DbMigrator.InitialDatabase, version1.MigrationId);
// Assert
Assert.True(script.Contains(CreateMetadataStatement));
Assert.True(script.Contains("Version1"));
Assert.False(script.Contains("AutomaticMigration"));
}
[MigrationsTheory]
[InlineData(true)]
[InlineData(false)]
public void Can_script_trailing_automatic_migration(bool whenDatabaseExists)
{
ResetDatabase();
var migrator1 = CreateMigrator<ShopContext_v1>();
var version1 = new MigrationScaffolder(migrator1.Configuration).Scaffold("Version1");
var migrator2 = CreateMigrator<ShopContext_v2>(automaticDataLossEnabled: true, scaffoldedMigrations: version1);
migrator2.Update();
var scriptingDecorator = new MigratorScriptingDecorator(migrator2);
if (!whenDatabaseExists)
{
ResetDatabase();
}
// Act
var script = scriptingDecorator.ScriptUpdate(version1.MigrationId, null);
// Assert
Assert.False(script.Contains(CreateMetadataStatement));
Assert.False(script.Contains("Version1"));
Assert.True(script.Contains("AutomaticMigration"));
}
[MigrationsTheory]
public void Can_script_downs()
{
ResetDatabase();
var version1 = new MigrationScaffolder(CreateMigrator<ShopContext_v1>().Configuration).Scaffold("Version1");
var migrator = CreateMigrator<ShopContext_v1>(scaffoldedMigrations: version1);
migrator.Update();
var scriptingDecorator = new MigratorScriptingDecorator(migrator);
var script = scriptingDecorator.ScriptUpdate(null, DbMigrator.InitialDatabase);
Assert.True(script.Contains(DropMetadataStatement));
Assert.False(script.Contains("Version1"));
}
[MigrationsTheory]
[InlineData(true)]
[InlineData(false)]
public void Can_script_using_migration_names(bool whenDatabaseExists)
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
var version1 = new MigrationScaffolder(migrator.Configuration).Scaffold("Banana");
CreateMigrator<ShopContext_v1>(scaffoldedMigrations: version1).Update();
migrator = CreateMigrator<ShopContext_v2>(scaffoldedMigrations: version1);
var version2 = new MigrationScaffolder(migrator.Configuration).Scaffold("Apple");
migrator = CreateMigrator<ShopContext_v2>(scaffoldedMigrations: new[] { version1, version2 });
migrator.Update();
if (!whenDatabaseExists)
{
ResetDatabase();
}
var scriptingDecorator
= new MigratorScriptingDecorator(
CreateMigrator<ShopContext_v2>(scaffoldedMigrations: new[] { version1, version2 }));
var script = scriptingDecorator.ScriptUpdate("Banana", "Apple");
Assert.False(script.Contains(CreateMetadataStatement));
Assert.False(script.Contains("Banana"));
Assert.True(script.Contains("Apple"));
Assert.False(script.Contains("AutomaticMigration"));
}
}
}

View File

@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.Migrations
{
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlServerCe, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.VB)]
public class DatabaseGeneratedScenarios : DbTestCase
{
[MigrationsTheory]
public void Can_auto_migrate_when_string_column_with_identity_database_generated_option()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v4>();
migrator.Update();
}
}
}

View File

@@ -0,0 +1,162 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.Migrations
{
using System.Data.Entity.Migrations.Model;
using System.Linq;
using Xunit;
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlServerCe, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.VB)]
public class DefaultValueScenarios : DbTestCase
{
private class DefaultValuesMigration : DbMigration
{
public override void Up()
{
CreateTable(
"DefaultValues",
c => new
{
Binary = c.Binary(defaultValue: new byte[] { }),
Boolean = c.Boolean(defaultValue: true),
Byte = c.Byte(defaultValue: 42),
DateTime = c.DateTime(defaultValue: new DateTime()),
DateTimeOffset = c.DateTimeOffset(defaultValue: new DateTimeOffset()),
Decimal = c.Decimal(defaultValue: 42.23m),
Double = c.Double(defaultValue: 123.45),
Guid = c.Guid(defaultValue: new Guid()),
Int = c.Int(defaultValue: 0),
Long = c.Long(defaultValue: 3456789),
Short = c.Short(defaultValue: 256),
Single = c.Single(defaultValue: 234.999f),
String = c.String(defaultValue: string.Empty),
Time = c.Time(defaultValue: TimeSpan.Zero)
});
}
}
[MigrationsTheory]
public void Can_create_columns_with_default_values_for_all_types()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
var defaultValuesMigration = new DefaultValuesMigration();
var createTableOperation
= (CreateTableOperation)defaultValuesMigration.GetOperations().Single();
WhenSqlCe(
() =>
{
createTableOperation.Columns.Remove(createTableOperation.Columns.Single(c => c.Name == "DateTimeOffset"));
createTableOperation.Columns.Remove(createTableOperation.Columns.Single(c => c.Name == "Time"));
});
migrator = CreateMigrator<ShopContext_v1>(defaultValuesMigration);
migrator.Update();
var table = Info.Tables.Single(t => t.Name == "DefaultValues");
Assert.True(table.Columns.Any(c => c.Name == "Binary" && c.Default.Contains("0x")));
Assert.True(table.Columns.Any(c => c.Name == "Boolean" && c.Default.Contains('1')));
Assert.True(table.Columns.Any(c => c.Name == "Byte" && c.Default.Contains("42")));
Assert.True(table.Columns.Any(c => c.Name == "DateTime" && c.Default.Contains("'0001-01-01T00:00:00.000'")));
Assert.True(table.Columns.Any(c => c.Name == "Decimal" && c.Default.Contains("42.23")));
Assert.True(table.Columns.Any(c => c.Name == "Double" && c.Default.Contains("123.45")));
Assert.True(table.Columns.Any(c => c.Name == "Guid" && c.Default.Contains("'00000000-0000-0000-0000-000000000000'")));
Assert.True(table.Columns.Any(c => c.Name == "Int" && c.Default.Contains("0")));
Assert.True(table.Columns.Any(c => c.Name == "Long" && c.Default.Contains("3456789")));
Assert.True(table.Columns.Any(c => c.Name == "Short" && c.Default.Contains("256")));
Assert.True(table.Columns.Any(c => c.Name == "Single" && c.Default.Contains("234.999")));
Assert.True(table.Columns.Any(c => c.Name == "String" && c.Default.Contains("''")));
WhenNotSqlCe(
() =>
{
Assert.True(table.Columns.Any(c => c.Name == "DateTimeOffset" && c.Default == "('0001-01-01T00:00:00.000+00:00')"));
Assert.True(table.Columns.Any(c => c.Name == "Time" && c.Default == "('00:00:00')"));
});
}
private class DefaultValueSqlMigration : DbMigration
{
public override void Up()
{
CreateTable(
"DefaultValueSql",
c => new
{
Binary = c.Binary(defaultValueSql: "CONVERT([binary],'123')"),
Boolean = c.Boolean(defaultValueSql: "CONVERT([bit],'1')"),
Byte = c.Byte(defaultValueSql: "CONVERT([tinyint],'123')"),
DateTime = c.DateTime(defaultValueSql: "CONVERT([datetime],'1947/08/15 03:33:20')"),
DateTimeOffset = c.DateTimeOffset(defaultValueSql: "CONVERT([datetimeoffset],'1947/08/15 03:33:20')"),
Decimal = c.Decimal(defaultValueSql: "CONVERT([money],'123')"),
Double = c.Double(defaultValueSql: "CONVERT([float],'123')"),
Guid = c.Guid(defaultValueSql: "CONVERT([uniqueidentifier],'123')"),
Int = c.Int(defaultValueSql: "CONVERT([int],'123')"),
Long = c.Long(defaultValueSql: "CONVERT([bigint],'123')"),
Short = c.Short(defaultValueSql: "CONVERT([smallint],'123')"),
Single = c.Single(defaultValueSql: "CONVERT([real],'123')"),
String = c.String(defaultValueSql: "CONVERT([nvarchar](100),(123))"),
Time = c.Time(defaultValueSql: "CONVERT([time],'03:33:20')")
});
}
}
[MigrationsTheory]
public void Can_create_columns_with_default_value_expressions_for_all_types()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
var defaultValuesMigration = new DefaultValueSqlMigration();
var createTableOperation
= (CreateTableOperation)defaultValuesMigration.GetOperations().Single();
WhenSqlCe(
() =>
{
createTableOperation.Columns.Remove(createTableOperation.Columns.Single(c => c.Name == "DateTimeOffset"));
createTableOperation.Columns.Remove(createTableOperation.Columns.Single(c => c.Name == "Time"));
});
migrator = CreateMigrator<ShopContext_v1>(defaultValuesMigration);
migrator.Update();
var table = Info.Tables.Single(t => t.Name == "DefaultValueSql");
Assert.True(table.Columns.Any(c => c.Name == "Binary" && c.Default.Contains("CONVERT([binary],'123'")));
Assert.True(table.Columns.Any(c => c.Name == "Boolean" && c.Default.Contains("CONVERT([bit],'1'")));
Assert.True(table.Columns.Any(c => c.Name == "Byte" && c.Default.Contains("CONVERT([tinyint],'123'")));
Assert.True(table.Columns.Any(c => c.Name == "DateTime" && c.Default.Contains("CONVERT([datetime],'1947/08/15 03:33:20'")));
Assert.True(table.Columns.Any(c => c.Name == "Decimal" && c.Default.Contains("CONVERT([money],'123'")));
Assert.True(table.Columns.Any(c => c.Name == "Double" && c.Default.Contains("CONVERT([float],'123'")));
Assert.True(table.Columns.Any(c => c.Name == "Guid" && c.Default.Contains("CONVERT([uniqueidentifier],'123'")));
Assert.True(table.Columns.Any(c => c.Name == "Int" && c.Default.Contains("CONVERT([int],'123'")));
Assert.True(table.Columns.Any(c => c.Name == "Long" && c.Default.Contains("CONVERT([bigint],'123'")));
Assert.True(table.Columns.Any(c => c.Name == "Short" && c.Default.Contains("CONVERT([smallint],'123'")));
Assert.True(table.Columns.Any(c => c.Name == "Single" && c.Default.Contains("CONVERT([real],'123'")));
Assert.True(table.Columns.Any(c => c.Name == "String" && c.Default.Contains("CONVERT([nvarchar](100),(123)")));
WhenNotSqlCe(
() =>
{
Assert.True(
table.Columns.Any(
c => c.Name == "DateTimeOffset" && c.Default.StartsWith("(CONVERT([datetimeoffset],'1947/08/15 03:33:20'")));
Assert.True(table.Columns.Any(c => c.Name == "Time" && c.Default.StartsWith("(CONVERT([time],'03:33:20'")));
});
}
}
}

View File

@@ -0,0 +1,38 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.Migrations
{
using Xunit;
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlServerCe, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.VB)]
public class DropColumnScenarios : DbTestCase
{
private class DropColumnMigration : DbMigration
{
public override void Up()
{
DropColumn("MigrationsProducts", "Name");
}
}
[MigrationsTheory]
public void Can_drop_column()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
Assert.True(ColumnExists("MigrationsProducts", "Name"));
migrator = CreateMigrator<ShopContext_v1>(new DropColumnMigration());
migrator.Update();
Assert.False(ColumnExists("MigrationsProducts", "Name"));
}
}
}

View File

@@ -0,0 +1,114 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.Migrations
{
using System.Linq;
using Xunit;
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlServerCe, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.VB)]
public class DropForeignKeyScenarios : DbTestCase
{
private class DropSimpleForeignKeyMigration : DbMigration
{
public override void Up()
{
DropForeignKey("dbo.OrderLines", "OrderId", "ordering.Orders");
}
}
[MigrationsTheory]
public void Can_drop_simple_foreign_key_constraint()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
Assert.True(Info.TableConstraints.Any(tc => tc.Name == "FK_dbo.OrderLines_ordering.Orders_OrderId"));
migrator = CreateMigrator<ShopContext_v1>(new DropSimpleForeignKeyMigration());
migrator.Update();
Assert.False(Info.TableConstraints.Any(tc => tc.Name == "FK_dbo.OrderLines_ordering.Orders_OrderId"));
}
private class DropCompositeForeignKeyMigration : DbMigration
{
public override void Up()
{
AddForeignKey("OrderLines", new[] { "ProductId", "Sku" }, "MigrationsProducts", new[] { "ProductId", "Sku" });
DropForeignKey("OrderLines", new[] { "ProductId", "Sku" }, "MigrationsProducts", new[] { "ProductId", "Sku" });
}
}
[MigrationsTheory]
public void Can_drop_composite_foreign_key_constraint()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1>(new DropCompositeForeignKeyMigration());
migrator.Update();
Assert.False(Info.TableConstraints.Any(tc => tc.Name == "FK_OrderLines_Products_ProductId_Sku"));
}
private class DropForeignKeyNoPrincipalMigration : DbMigration
{
public override void Up()
{
AddForeignKey("OrderLines", new[] { "ProductId", "Sku" }, "MigrationsProducts");
DropForeignKey("OrderLines", new[] { "ProductId", "Sku" }, "MigrationsProducts");
}
}
[MigrationsTheory]
public void Can_drop_composite_foreign_key_constraint_when_principal_columns_not_specified()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1>(new DropForeignKeyNoPrincipalMigration());
migrator.Update();
Assert.False(Info.TableConstraints.Any(tc => tc.Name == "FK_OrderLines_Products_ProductId_Sku"));
}
private class DropForeignKeyWithName : DbMigration
{
public override void Up()
{
AddForeignKey("OrderLines", new[] { "ProductId", "Sku" }, "MigrationsProducts", name: "TheFK");
DropForeignKey("OrderLines", "TheFK");
}
}
[MigrationsTheory]
public void Can_drop_composite_foreign_key_constraint_when_name_specified()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1>(new DropForeignKeyWithName());
migrator.Update();
Assert.False(Info.TableConstraints.Any(tc => tc.Name == "TheFK"));
}
}
}

View File

@@ -0,0 +1,79 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.Migrations
{
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlServerCe, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.VB)]
public class DropIndexScenarios : DbTestCase
{
private class DropSimpleIndexMigration : DbMigration
{
public override void Up()
{
CreateIndex("OrderLines", "OrderId");
DropIndex("OrderLines", new[] { "OrderId" });
}
}
[MigrationsTheory]
public void Can_drop_simple_index()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1, DropSimpleIndexMigration>();
migrator.Update();
}
private class DropCompositeIndexMigration : DbMigration
{
public override void Up()
{
CreateIndex("OrderLines", new[] { "ProductId", "Sku" });
DropIndex("OrderLines", new[] { "ProductId", "Sku" });
}
}
[MigrationsTheory]
public void Can_drop_composite_index()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1, DropCompositeIndexMigration>();
migrator.Update();
}
private class DropIndexWithName : DbMigration
{
public override void Up()
{
CreateIndex("OrderLines", new[] { "ProductId", "Sku" }, false, name: "TheIndex");
DropIndex("OrderLines", "TheIndex");
}
}
[MigrationsTheory]
public void Can_drop_composite_index_when_name_specified()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<ShopContext_v1, DropIndexWithName>();
migrator.Update();
}
}
}

View File

@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.Migrations
{
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlServerCe, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.VB)]
public class MappingScenarios : DbTestCase
{
[MigrationsTheory]
public void Can_migrate_model_with_advanced_mapping_scenarios()
{
ResetDatabase();
var migrator = CreateMigrator<MappingScenariosContext>();
migrator.Update();
}
}
}

View File

@@ -0,0 +1,226 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.Migrations
{
using System.Data.Entity.Migrations.Design;
using System.Data.Entity.Migrations.History;
using Xunit;
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlServerCe, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.VB)]
public class MultiTenantScenarios : DbTestCase
{
public class ContextA : DbContext
{
public DbSet<TenantA> As { get; set; }
}
public class ContextA2 : ContextA
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("foo");
}
}
public class ContextB : DbContext
{
public DbSet<TenantB> Bs { get; set; }
}
public class ContextB2 : ContextB
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("foo");
}
}
[MigrationsTheory]
public void Can_update_when_explicit_migrations()
{
ResetDatabase();
var migratorA = CreateMigrator<ContextA>();
var generatedMigrationA = new MigrationScaffolder(migratorA.Configuration).Scaffold("MigrationA");
migratorA
= CreateMigrator<ContextA>(
contextKey: "KeyA",
automaticMigrationsEnabled: false,
scaffoldedMigrations: generatedMigrationA);
migratorA.Update();
var migratorB = CreateMigrator<ContextB>();
var generatedMigrationB = new MigrationScaffolder(migratorB.Configuration).Scaffold("MigrationB");
migratorB
= CreateMigrator<ContextB>(
contextKey: "KeyB",
automaticMigrationsEnabled: false,
scaffoldedMigrations: generatedMigrationB);
migratorB.Update();
Assert.True(TableExists("TenantAs"));
Assert.True(TableExists("TenantBs"));
migratorA.Update("0");
migratorB.Update("0");
Assert.False(TableExists("TenantAs"));
Assert.False(TableExists("TenantBs"));
}
[MigrationsTheory]
public void Can_update_when_auto_migrations()
{
ResetDatabase();
var migratorA
= CreateMigrator<ContextA>(contextKey: "KeyA", automaticDataLossEnabled: true);
migratorA.Update();
var migratorB
= CreateMigrator<ContextB>(contextKey: "KeyB", automaticDataLossEnabled: true);
migratorB.Update();
Assert.True(TableExists("TenantAs"));
Assert.True(TableExists("TenantBs"));
migratorA.Update("0");
migratorB.Update("0");
Assert.False(TableExists("TenantAs"));
Assert.False(TableExists("TenantBs"));
}
[MigrationsTheory]
public void Can_initialize_with_create_if_not_exists()
{
ResetDatabase();
Database.SetInitializer(new CreateDatabaseIfNotExists<ContextA>());
Database.SetInitializer(new CreateDatabaseIfNotExists<ContextB>());
CreateContext<ContextA>().Database.Initialize(true);
CreateContext<ContextB>().Database.Initialize(true);
Assert.True(TableExists("TenantAs"));
Assert.True(TableExists("TenantBs"));
}
}
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.VB)]
public class MultitenantScenarios_NoSqlCe : DbTestCase
{
[MigrationsTheory]
public void Can_update_when_default_schema_introduced()
{
ResetDatabase();
var migratorA
= CreateMigrator<MultiTenantScenarios.ContextA>(contextKey: "KeyA");
var generatedMigrationA1
= new MigrationScaffolder(migratorA.Configuration).Scaffold("MigrationA");
migratorA
= CreateMigrator<MultiTenantScenarios.ContextA>(
contextKey: "KeyA",
automaticMigrationsEnabled: false,
scaffoldedMigrations: generatedMigrationA1);
migratorA.Update();
Assert.True(TableExists("dbo.TenantAs"));
Assert.True(TableExists("dbo." + HistoryContext.TableName));
var migratorB
= CreateMigrator<MultiTenantScenarios.ContextB>(contextKey: "KeyB");
var generatedMigrationB1
= new MigrationScaffolder(migratorB.Configuration).Scaffold("MigrationB");
migratorB
= CreateMigrator<MultiTenantScenarios.ContextB>(
contextKey: "KeyB",
automaticMigrationsEnabled: false,
scaffoldedMigrations: generatedMigrationB1);
migratorB.Update();
Assert.True(TableExists("dbo.TenantBs"));
Assert.True(TableExists("dbo." + HistoryContext.TableName));
migratorA
= CreateMigrator<MultiTenantScenarios.ContextA2>(
contextKey: "KeyA",
scaffoldedMigrations: generatedMigrationA1);
var generatedMigrationA2
= new MigrationScaffolder(migratorA.Configuration).Scaffold("MigrationA2");
migratorA
= CreateMigrator<MultiTenantScenarios.ContextA2>(
contextKey: "KeyA",
automaticMigrationsEnabled: false,
scaffoldedMigrations: new[] { generatedMigrationA1, generatedMigrationA2 });
migratorA.Update();
Assert.True(TableExists("foo.TenantAs"));
Assert.True(TableExists("foo." + HistoryContext.TableName));
Assert.False(TableExists("dbo.TenantAs"));
Assert.True(TableExists("dbo." + HistoryContext.TableName));
migratorB
= CreateMigrator<MultiTenantScenarios.ContextB2>(
contextKey: "KeyB",
scaffoldedMigrations: generatedMigrationB1);
var generatedMigrationB2
= new MigrationScaffolder(migratorB.Configuration).Scaffold("MigrationB2");
migratorB
= CreateMigrator<MultiTenantScenarios.ContextB2>(
contextKey: "KeyB",
automaticMigrationsEnabled: false,
scaffoldedMigrations: new[] { generatedMigrationB1, generatedMigrationB2 });
migratorB.Update();
Assert.True(TableExists("foo.TenantBs"));
Assert.True(TableExists("foo." + HistoryContext.TableName));
Assert.False(TableExists("dbo.TenantBs"));
Assert.False(TableExists("dbo." + HistoryContext.TableName));
migratorA.Update("0");
Assert.False(TableExists("foo.TenantAs"));
Assert.True(TableExists("foo." + HistoryContext.TableName));
migratorB.Update("0");
Assert.False(TableExists("foo.TenantBs"));
Assert.False(TableExists("foo." + HistoryContext.TableName));
}
}
public class TenantA
{
public int Id { get; set; }
}
public class TenantB
{
public int Id { get; set; }
}
}

View File

@@ -0,0 +1,32 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.Migrations
{
using System.Data.Entity.Migrations.Design;
using Xunit;
public class NoTestInfraScenarios : TestBase
{
[Fact]
public void Can_generate_migration_from_user_code()
{
var migrator
= new DbMigrator(
new DbMigrationsConfiguration
{
ContextType = typeof(ShopContext_v1),
MigrationsAssembly = SystemComponentModelDataAnnotationsAssembly,
MigrationsNamespace = "Foo",
MigrationsDirectory = "Bar"
});
var migration = new MigrationScaffolder(migrator.Configuration).Scaffold("Test");
Assert.False(string.IsNullOrWhiteSpace(migration.DesignerCode));
Assert.False(string.IsNullOrWhiteSpace(migration.Language));
Assert.False(string.IsNullOrWhiteSpace(migration.MigrationId));
Assert.False(string.IsNullOrWhiteSpace(migration.UserCode));
Assert.False(string.IsNullOrWhiteSpace(migration.Directory));
}
}
}

View File

@@ -0,0 +1,262 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.Migrations
{
using System.Data.Entity.Migrations.Design;
using System.Data.Entity.Migrations.History;
using System.Data.Entity.Migrations.Infrastructure;
using Xunit;
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlServerCe, ProgrammingLanguage.CSharp)]
[Variant(DatabaseProvider.SqlClient, ProgrammingLanguage.VB)]
public class SchemaScenarios : DbTestCase
{
private class CustomSchemaContext_v1 : ShopContext_v1
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.HasDefaultSchema("foo");
}
}
private class CustomSchemaContext_v1b : CustomSchemaContext_v1
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<MigrationsCustomer>().ToTable("tbl_customers", "crm");
}
}
private class CustomSchemaContext_v2 : ShopContext_v1
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.HasDefaultSchema("bar");
}
}
[MigrationsTheory]
public void Can_generate_and_update_when_custom_default_schemas()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
var generatedMigration0 = new MigrationScaffolder(migrator.Configuration).Scaffold("Migration_v0");
migrator = CreateMigrator<ShopContext_v1>(false, scaffoldedMigrations: generatedMigration0);
migrator.Update();
Assert.True(TableExists("dbo.OrderLines"));
Assert.True(TableExists("ordering.Orders"));
Assert.True(TableExists("dbo." + HistoryContext.TableName));
migrator = CreateMigrator<CustomSchemaContext_v1>();
var generatedMigration1 = new MigrationScaffolder(migrator.Configuration).Scaffold("Migration_v1");
migrator = CreateMigrator<CustomSchemaContext_v1>(
false, scaffoldedMigrations: new[] { generatedMigration0, generatedMigration1 });
migrator.Update();
WhenNotSqlCe(
() =>
{
Assert.False(TableExists("dbo.OrderLines"));
Assert.False(TableExists("dbo." + HistoryContext.TableName));
});
Assert.True(TableExists("foo.OrderLines"));
Assert.True(TableExists("ordering.Orders"));
Assert.True(TableExists("foo." + HistoryContext.TableName));
migrator = CreateMigrator<CustomSchemaContext_v2>(scaffoldedMigrations: generatedMigration1);
var generatedMigration2 = new MigrationScaffolder(migrator.Configuration).Scaffold("Migration_v2");
migrator
= CreateMigrator<CustomSchemaContext_v2>(
false, scaffoldedMigrations: new[] { generatedMigration0, generatedMigration1, generatedMigration2 });
migrator.Update();
WhenNotSqlCe(
() =>
{
Assert.False(TableExists("foo.OrderLines"));
Assert.False(TableExists("foo." + HistoryContext.TableName));
});
Assert.True(TableExists("bar.OrderLines"));
Assert.True(TableExists("ordering.Orders"));
Assert.True(TableExists("bar." + HistoryContext.TableName));
migrator.Update("0");
Assert.False(TableExists("foo.OrderLines"));
Assert.False(TableExists("ordering.Orders"));
Assert.False(TableExists("foo." + HistoryContext.TableName));
}
[MigrationsTheory]
public void Can_generate_and_update_clean_database_when_custom_default_schemas()
{
ResetDatabase();
var migrator = CreateMigrator<CustomSchemaContext_v1>();
var generatedMigration1 = new MigrationScaffolder(migrator.Configuration).Scaffold("Migration_v1");
migrator = CreateMigrator<CustomSchemaContext_v1>(false, scaffoldedMigrations: generatedMigration1);
migrator.Update();
Assert.True(TableExists("foo.OrderLines"));
Assert.True(TableExists("ordering.Orders"));
Assert.True(TableExists("foo." + HistoryContext.TableName));
migrator = CreateMigrator<CustomSchemaContext_v2>(scaffoldedMigrations: generatedMigration1);
var generatedMigration2 = new MigrationScaffolder(migrator.Configuration).Scaffold("Migration_v2");
migrator
= CreateMigrator<CustomSchemaContext_v2>(
false, scaffoldedMigrations: new[] { generatedMigration1, generatedMigration2 });
ResetDatabase();
migrator.Update();
WhenNotSqlCe(
() =>
{
Assert.False(TableExists("foo.OrderLines"));
Assert.False(TableExists("foo." + HistoryContext.TableName));
});
Assert.True(TableExists("bar.OrderLines"));
Assert.True(TableExists("ordering.Orders"));
Assert.True(TableExists("bar." + HistoryContext.TableName));
migrator.Update("0");
Assert.False(TableExists("foo.OrderLines"));
Assert.False(TableExists("ordering.Orders"));
Assert.False(TableExists("foo." + HistoryContext.TableName));
}
[MigrationsTheory]
public void Can_auto_update_after_custom_default_schema_introduced()
{
ResetDatabase();
var migrator = CreateMigrator<CustomSchemaContext_v1>();
var generatedMigration = new MigrationScaffolder(migrator.Configuration).Scaffold("Migration_v1");
migrator = CreateMigrator<CustomSchemaContext_v1>(false, scaffoldedMigrations: generatedMigration);
migrator.Update();
Assert.True(TableExists("foo.OrderLines"));
Assert.True(TableExists("ordering.Orders"));
Assert.True(TableExists("foo." + HistoryContext.TableName));
migrator = CreateMigrator<CustomSchemaContext_v1b>(scaffoldedMigrations: generatedMigration);
migrator.Update();
Assert.True(TableExists("crm.tbl_customers"));
migrator.Update("0");
Assert.False(TableExists("crm.tbl_customers"));
Assert.False(TableExists("foo.OrderLines"));
Assert.False(TableExists("ordering.Orders"));
Assert.False(TableExists("foo." + HistoryContext.TableName));
}
[MigrationsTheory]
public void Can_auto_update_before_custom_default_schema_introduced()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
Assert.True(TableExists("dbo." + HistoryContext.TableName));
migrator = CreateMigrator<CustomSchemaContext_v1>();
var generatedMigration = new MigrationScaffolder(migrator.Configuration).Scaffold("Migration_v1");
migrator = CreateMigrator<CustomSchemaContext_v1>(scaffoldedMigrations: generatedMigration, automaticDataLossEnabled: true);
migrator.Update();
WhenNotSqlCe(
() =>
{
Assert.True(TableExists("foo." + HistoryContext.TableName));
Assert.False(TableExists("dbo." + HistoryContext.TableName));
});
migrator.Update("0");
Assert.False(TableExists("foo." + HistoryContext.TableName));
Assert.False(TableExists("dbo." + HistoryContext.TableName));
}
[MigrationsTheory]
public void Auto_update_when_custom_default_schema_should_throw()
{
ResetDatabase();
var migrator = CreateMigrator<CustomSchemaContext_v1>();
Assert.Throws<MigrationsException>(() => migrator.Update())
.ValidateMessage("UnableToMoveHistoryTableWithAuto");
}
[MigrationsTheory]
public void Auto_update_when_custom_default_schema_introduced_should_throw()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
migrator = CreateMigrator<CustomSchemaContext_v1>();
Assert.Throws<MigrationsException>(() => migrator.Update())
.ValidateMessage("UnableToMoveHistoryTableWithAuto");
}
[MigrationsTheory]
public void Can_get_database_migrations_when_custom_default_schema_introduced()
{
ResetDatabase();
var migrator = CreateMigrator<ShopContext_v1>();
migrator.Update();
Assert.True(TableExists("dbo." + HistoryContext.TableName));
migrator = CreateMigrator<CustomSchemaContext_v1>();
Assert.NotEmpty(migrator.GetDatabaseMigrations());
}
}
}

Some files were not shown because too many files have changed in this diff Show More