From e8fc5a970af4873904d2373a9244750bb201a357 Mon Sep 17 00:00:00 2001 From: Brice Lambson Date: Mon, 17 Aug 2015 10:24:21 -0700 Subject: [PATCH] React to aspnet/EntityFramework#2831 --- .../DatabaseErrorPageMiddleware.cs | 14 ++++-- .../DatabaseErrorPageMiddlewareTest.cs | 10 +---- ...t.Diagnostics.Entity.FunctionalTests.xproj | 3 ++ .../TestModels/BloggingContext.cs | 12 +---- .../BloggingContextWithMigrations.cs | 45 ++++++------------- .../BloggingContextWithPendingModelChanges.cs | 22 ++------- .../BloggingContextWithSnapshotThatThrows.cs | 27 +++-------- 7 files changed, 41 insertions(+), 92 deletions(-) diff --git a/src/Microsoft.AspNet.Diagnostics.Entity/DatabaseErrorPageMiddleware.cs b/src/Microsoft.AspNet.Diagnostics.Entity/DatabaseErrorPageMiddleware.cs index 9f326237ec..de22582576 100644 --- a/src/Microsoft.AspNet.Diagnostics.Entity/DatabaseErrorPageMiddleware.cs +++ b/src/Microsoft.AspNet.Diagnostics.Entity/DatabaseErrorPageMiddleware.cs @@ -80,11 +80,19 @@ namespace Microsoft.AspNet.Diagnostics.Entity { var databaseExists = creator.Exists(); - var migrator = dbContext.GetService(); + var historyRepository = dbContext.GetService(); + var migrationsAssembly = dbContext.GetService(); + var modelDiffer = dbContext.GetService(); - var pendingMigrations = migrator.GetUnappliedMigrations().Select(m => m.Id); + var appliedMigrations = historyRepository.GetAppliedMigrations(); + var pendingMigrations = ( + from m in migrationsAssembly.Migrations + where !appliedMigrations.Any( + r => string.Equals(r.MigrationId, m.Id, StringComparison.OrdinalIgnoreCase)) + select m.Id) + .ToList(); - var pendingModelChanges = migrator.HasPendingModelChanges(); + var pendingModelChanges = modelDiffer.HasDifferences(migrationsAssembly.ModelSnapshot?.Model, dbContext.Model); if ((!databaseExists && pendingMigrations.Any()) || pendingMigrations.Any() || pendingModelChanges) { diff --git a/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/DatabaseErrorPageMiddlewareTest.cs b/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/DatabaseErrorPageMiddlewareTest.cs index c1ce6dfc1a..d4ae76b480 100644 --- a/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/DatabaseErrorPageMiddlewareTest.cs +++ b/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/DatabaseErrorPageMiddlewareTest.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Net; @@ -10,19 +9,14 @@ using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Diagnostics.Entity.FunctionalTests.Helpers; +using Microsoft.AspNet.Diagnostics.Entity.Tests.Helpers; using Microsoft.AspNet.Http; using Microsoft.AspNet.TestHost; using Microsoft.Data.Entity; using Microsoft.Data.Entity.Infrastructure; -using Microsoft.Data.Entity.Internal; -using Microsoft.Data.Entity.Migrations; -using Microsoft.Data.Entity.Migrations.Infrastructure; -using Microsoft.Data.Entity.Storage; -using Microsoft.Data.Entity.Utilities; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.Logging; using Xunit; -using Microsoft.AspNet.Diagnostics.Entity.Tests.Helpers; namespace Microsoft.AspNet.Diagnostics.Entity.Tests { @@ -162,7 +156,7 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests { using (var db = context.ApplicationServices.GetService()) { - db.Database.ApplyMigrations(); + db.Database.Migrate(); db.Blogs.Add(new Blog()); db.SaveChanges(); diff --git a/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests.xproj b/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests.xproj index 74e10116a8..84e0caea66 100644 --- a/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests.xproj +++ b/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests.xproj @@ -13,5 +13,8 @@ 2.0 + + + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/TestModels/BloggingContext.cs b/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/TestModels/BloggingContext.cs index 8e721980ae..d8b96f7b96 100644 --- a/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/TestModels/BloggingContext.cs +++ b/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/TestModels/BloggingContext.cs @@ -1,24 +1,14 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using Microsoft.Data.Entity; using Microsoft.Data.Entity.Infrastructure; -using Microsoft.Data.Entity.Migrations; -using System; -using System.Linq; -using System.Reflection; namespace Microsoft.AspNet.Diagnostics.Entity.Tests { public class BloggingContext : DbContext { - protected static readonly string CurrentProductVersion = typeof(Migrator) - .GetTypeInfo() - .Assembly - .GetCustomAttributes() - .Single() - .InformationalVersion; - protected BloggingContext(DbContextOptions options) : base(options) { } diff --git a/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/TestModels/BloggingContextWithMigrations.cs b/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/TestModels/BloggingContextWithMigrations.cs index f690f9ff3e..4b75c38b8b 100644 --- a/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/TestModels/BloggingContextWithMigrations.cs +++ b/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/TestModels/BloggingContextWithMigrations.cs @@ -4,9 +4,9 @@ using System; using Microsoft.Data.Entity; using Microsoft.Data.Entity.Infrastructure; +using Microsoft.Data.Entity.Metadata; using Microsoft.Data.Entity.Migrations; -using Microsoft.Data.Entity.Migrations.Builders; -using Microsoft.Data.Entity.Migrations.Infrastructure; +using Microsoft.Data.Entity.SqlServer.Metadata; namespace Microsoft.AspNet.Diagnostics.Entity.Tests { @@ -26,10 +26,10 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests return new BloggingContextWithMigrations(options); } - [ContextType(typeof(BloggingContextWithMigrations))] + [DbContext(typeof(BloggingContextWithMigrations))] public class BloggingContextWithMigrationsModelSnapshot : ModelSnapshot { - public override void BuildModel(ModelBuilder builder) + protected override void BuildModel(ModelBuilder builder) { builder.Entity("Blogging.Models.Blog", b => { @@ -40,7 +40,7 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests } } - [ContextType(typeof(BloggingContextWithMigrations))] + [DbContext(typeof(BloggingContextWithMigrations))] public class MigrationOne : Migration { public override string Id @@ -48,34 +48,26 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests get { return "111111111111111_MigrationOne"; } } - public override string ProductVersion - { - get { return CurrentProductVersion; } - } + public override IModel TargetModel => new BloggingContextWithMigrationsModelSnapshot().Model; - public override void BuildTargetModel(ModelBuilder modelBuilder) - { - new BloggingContextWithMigrationsModelSnapshot().BuildModel(modelBuilder); - } - - public override void Up(MigrationBuilder migrationBuilder) + protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateTable("Blog", c => new { - BlogId = c.Column("int").Annotation("SqlServer:ValueGeneration", "Identity"), - Name = c.Column("nvarchar(max)", nullable: true), + BlogId = c.Column().Annotation("SqlServer:ValueGenerationStrategy", SqlServerIdentityStrategy.IdentityColumn), + Name = c.Column(isNullable: true), }) .PrimaryKey("PK_Blog", t => t.BlogId); } - public override void Down(MigrationBuilder migrationBuilder) + protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropTable("Blog"); } } - [ContextType(typeof(BloggingContextWithMigrations))] + [DbContext(typeof(BloggingContextWithMigrations))] public class MigrationTwo : Migration { public override string Id @@ -83,20 +75,9 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests get { return "222222222222222_MigrationTwo"; } } - public override string ProductVersion - { - get { return CurrentProductVersion; } - } + public override IModel TargetModel => new BloggingContextWithMigrationsModelSnapshot().Model; - public override void BuildTargetModel(ModelBuilder modelBuilder) - { - new BloggingContextWithMigrationsModelSnapshot().BuildModel(modelBuilder); - } - - public override void Up(MigrationBuilder migrationBuilder) - { } - - public override void Down(MigrationBuilder migrationBuilder) + protected override void Up(MigrationBuilder migrationBuilder) { } } } diff --git a/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/TestModels/BloggingContextWithPendingModelChanges.cs b/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/TestModels/BloggingContextWithPendingModelChanges.cs index 3daf836ef4..0bf5a12244 100644 --- a/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/TestModels/BloggingContextWithPendingModelChanges.cs +++ b/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/TestModels/BloggingContextWithPendingModelChanges.cs @@ -5,8 +5,6 @@ using System; using Microsoft.Data.Entity; using Microsoft.Data.Entity.Infrastructure; using Microsoft.Data.Entity.Migrations; -using Microsoft.Data.Entity.Migrations.Builders; -using Microsoft.Data.Entity.Migrations.Infrastructure; namespace Microsoft.AspNet.Diagnostics.Entity.Tests { @@ -16,15 +14,15 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests : base(provider, options) { } - [ContextType(typeof(BloggingContextWithPendingModelChanges))] + [DbContext(typeof(BloggingContextWithPendingModelChanges))] public class BloggingModelSnapshot : ModelSnapshot { - public override void BuildModel(ModelBuilder modelBuilder) + protected override void BuildModel(ModelBuilder modelBuilder) { } } - [ContextType(typeof(BloggingContextWithPendingModelChanges))] + [DbContext(typeof(BloggingContextWithPendingModelChanges))] public partial class MigrationOne : Migration { public override string Id @@ -32,19 +30,7 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests get { return "111111111111111_MigrationOne"; } } - public override string ProductVersion - { - get { return CurrentProductVersion; } - } - - public override void BuildTargetModel(ModelBuilder modelBuilder) - { - } - - public override void Up(MigrationBuilder migrationBuilder) - { } - - public override void Down(MigrationBuilder migrationBuilder) + protected override void Up(MigrationBuilder migrationBuilder) { } } } diff --git a/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/TestModels/BloggingContextWithSnapshotThatThrows.cs b/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/TestModels/BloggingContextWithSnapshotThatThrows.cs index 665a04994c..dea848d0fa 100644 --- a/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/TestModels/BloggingContextWithSnapshotThatThrows.cs +++ b/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/TestModels/BloggingContextWithSnapshotThatThrows.cs @@ -1,13 +1,11 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using Microsoft.Data.Entity; using Microsoft.Data.Entity.Infrastructure; +using Microsoft.Data.Entity.Metadata; using Microsoft.Data.Entity.Migrations; -using Microsoft.Data.Entity.Migrations.Builders; -using Microsoft.Data.Entity.Migrations.Infrastructure; -using System; - namespace Microsoft.AspNet.Diagnostics.Entity.Tests { @@ -17,16 +15,16 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests : base(provider, options) { } - [ContextType(typeof(BloggingContextWithSnapshotThatThrows))] + [DbContext(typeof(BloggingContextWithSnapshotThatThrows))] public class BloggingContextWithSnapshotThatThrowsModelSnapshot : ModelSnapshot { - public override void BuildModel(ModelBuilder modelBuilder) + protected override void BuildModel(ModelBuilder modelBuilder) { throw new Exception("Welcome to the invalid snapshot!"); } } - [ContextType(typeof(BloggingContextWithSnapshotThatThrows))] + [DbContext(typeof(BloggingContextWithSnapshotThatThrows))] public class MigrationOne : Migration { public override string Id @@ -34,23 +32,12 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests get { return "111111111111111_MigrationOne"; } } - public override string ProductVersion - { - get { return CurrentProductVersion; } - } + public override IModel TargetModel => new BloggingContextWithSnapshotThatThrowsModelSnapshot().Model; - public override void BuildTargetModel(ModelBuilder modelBuilder) - { - new BloggingContextWithSnapshotThatThrowsModelSnapshot().BuildModel(modelBuilder); - } - - public override void Up(MigrationBuilder migrationBuilder) + protected override void Up(MigrationBuilder migrationBuilder) { throw new Exception("Welcome to the invalid migration!"); } - - public override void Down(MigrationBuilder migrationBuilder) - { } } } } \ No newline at end of file