From 16704de8eb0a5cb192ad2c488124a2fd7cbd7af0 Mon Sep 17 00:00:00 2001 From: Brice Lambson Date: Tue, 9 Mar 2021 13:54:08 -0800 Subject: [PATCH] Add missing design-time services --- .../DesignTimeServiceCollectionExtensions.cs | 33 ++++++--- .../Internal/SqlServerDesignTimeServices.cs | 13 +++- .../Properties/AssemblyInfo.cs | 8 --- .../Internal/SqliteDesignTimeServices.cs | 13 +++- .../Properties/AssemblyInfo.cs | 8 --- .../DesignTimeTestBase.cs | 68 +++++++++++++++++++ ...Core.Relational.Specification.Tests.csproj | 1 + .../DesignTimeSqlServerTest.cs | 26 +++++++ .../EFCore.SqlServer.FunctionalTests.csproj | 1 - .../DesignTimeSqliteTest.cs | 26 +++++++ 10 files changed, 165 insertions(+), 32 deletions(-) delete mode 100644 src/EFCore.SqlServer/Properties/AssemblyInfo.cs delete mode 100644 src/EFCore.Sqlite.Core/Properties/AssemblyInfo.cs create mode 100644 test/EFCore.Relational.Specification.Tests/DesignTimeTestBase.cs create mode 100644 test/EFCore.SqlServer.FunctionalTests/DesignTimeSqlServerTest.cs create mode 100644 test/EFCore.Sqlite.FunctionalTests/DesignTimeSqliteTest.cs diff --git a/src/EFCore.Design/Design/DesignTimeServiceCollectionExtensions.cs b/src/EFCore.Design/Design/DesignTimeServiceCollectionExtensions.cs index 88842a71f18..4f817b0d0ac 100644 --- a/src/EFCore.Design/Design/DesignTimeServiceCollectionExtensions.cs +++ b/src/EFCore.Design/Design/DesignTimeServiceCollectionExtensions.cs @@ -3,12 +3,15 @@ using System; using System.Diagnostics; +using System.Linq; using JetBrains.Annotations; using Microsoft.EntityFrameworkCore.Design.Internal; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Diagnostics.Internal; using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Internal; using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Migrations.Design; using Microsoft.EntityFrameworkCore.Migrations.Internal; @@ -50,37 +53,49 @@ public static IServiceCollection AddEntityFrameworkDesignTimeServices( .AddSingleton() .AddSingleton() .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() + .AddSingleton(new DiagnosticListener(DbLoggerCategory.Name)) + .AddSingleton() .AddSingleton() + .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() + .AddSingleton() .AddSingleton(typeof(IDiagnosticsLogger<>), typeof(DiagnosticsLogger<>)) .AddSingleton() - .AddSingleton(new DiagnosticListener(DbLoggerCategory.Name)) .AddSingleton() + .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() - .AddSingleton() + .AddSingleton() + .AddSingleton() .AddSingleton( new DesignTimeConnectionStringResolver(applicationServiceProviderAccessor)) .AddSingleton(reporter) + .AddSingleton() .AddSingleton() + .AddSingleton() + .AddSingleton(new RegisteredServices(services.Select(s => s.ServiceType))) .AddSingleton() .AddSingleton() .AddSingleton() + .AddSingleton(p => p.GetService()) .AddSingleton() - .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() .AddTransient() .AddTransient() .AddTransient() diff --git a/src/EFCore.SqlServer/Design/Internal/SqlServerDesignTimeServices.cs b/src/EFCore.SqlServer/Design/Internal/SqlServerDesignTimeServices.cs index 1210c28ef1d..a05610d9804 100644 --- a/src/EFCore.SqlServer/Design/Internal/SqlServerDesignTimeServices.cs +++ b/src/EFCore.SqlServer/Design/Internal/SqlServerDesignTimeServices.cs @@ -3,8 +3,12 @@ using Microsoft.EntityFrameworkCore.Design; using Microsoft.EntityFrameworkCore.Diagnostics; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Scaffolding; using Microsoft.EntityFrameworkCore.SqlServer.Diagnostics.Internal; +using Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal; +using Microsoft.EntityFrameworkCore.SqlServer.Metadata.Internal; using Microsoft.EntityFrameworkCore.SqlServer.Scaffolding.Internal; using Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal; using Microsoft.EntityFrameworkCore.Storage; @@ -12,6 +16,8 @@ #nullable enable +[assembly: DesignTimeProviderServices("Microsoft.EntityFrameworkCore.SqlServer.Design.Internal.SqlServerDesignTimeServices")] + namespace Microsoft.EntityFrameworkCore.SqlServer.Design.Internal { /// @@ -30,10 +36,11 @@ public class SqlServerDesignTimeServices : IDesignTimeServices /// public virtual void ConfigureDesignTimeServices(IServiceCollection serviceCollection) => serviceCollection - .AddSingleton() - .AddSingleton() + .AddSingleton() .AddSingleton() + .AddSingleton() .AddSingleton() - .AddSingleton(); + .AddSingleton() + .AddSingleton(); } } diff --git a/src/EFCore.SqlServer/Properties/AssemblyInfo.cs b/src/EFCore.SqlServer/Properties/AssemblyInfo.cs deleted file mode 100644 index 6e1bf2c78f2..00000000000 --- a/src/EFCore.SqlServer/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,8 +0,0 @@ -// 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 Microsoft.EntityFrameworkCore.Design; - -#nullable enable - -[assembly: DesignTimeProviderServices("Microsoft.EntityFrameworkCore.SqlServer.Design.Internal.SqlServerDesignTimeServices")] diff --git a/src/EFCore.Sqlite.Core/Design/Internal/SqliteDesignTimeServices.cs b/src/EFCore.Sqlite.Core/Design/Internal/SqliteDesignTimeServices.cs index c9b27f6c800..2d0fff49d89 100644 --- a/src/EFCore.Sqlite.Core/Design/Internal/SqliteDesignTimeServices.cs +++ b/src/EFCore.Sqlite.Core/Design/Internal/SqliteDesignTimeServices.cs @@ -3,8 +3,12 @@ using Microsoft.EntityFrameworkCore.Design; using Microsoft.EntityFrameworkCore.Diagnostics; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Scaffolding; using Microsoft.EntityFrameworkCore.Sqlite.Diagnostics.Internal; +using Microsoft.EntityFrameworkCore.Sqlite.Infrastructure.Internal; +using Microsoft.EntityFrameworkCore.Sqlite.Metadata.Internal; using Microsoft.EntityFrameworkCore.Sqlite.Scaffolding.Internal; using Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal; using Microsoft.EntityFrameworkCore.Storage; @@ -12,6 +16,8 @@ #nullable enable +[assembly: DesignTimeProviderServices("Microsoft.EntityFrameworkCore.Sqlite.Design.Internal.SqliteDesignTimeServices")] + namespace Microsoft.EntityFrameworkCore.Sqlite.Design.Internal { /// @@ -30,10 +36,11 @@ public class SqliteDesignTimeServices : IDesignTimeServices /// public virtual void ConfigureDesignTimeServices(IServiceCollection serviceCollection) => serviceCollection - .AddSingleton() - .AddSingleton() + .AddSingleton() .AddSingleton() + .AddSingleton() .AddSingleton() - .AddSingleton(); + .AddSingleton() + .AddSingleton(); } } diff --git a/src/EFCore.Sqlite.Core/Properties/AssemblyInfo.cs b/src/EFCore.Sqlite.Core/Properties/AssemblyInfo.cs deleted file mode 100644 index 7c04ea88200..00000000000 --- a/src/EFCore.Sqlite.Core/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,8 +0,0 @@ -// 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 Microsoft.EntityFrameworkCore.Design; - -#nullable enable - -[assembly: DesignTimeProviderServices("Microsoft.EntityFrameworkCore.Sqlite.Design.Internal.SqliteDesignTimeServices")] diff --git a/test/EFCore.Relational.Specification.Tests/DesignTimeTestBase.cs b/test/EFCore.Relational.Specification.Tests/DesignTimeTestBase.cs new file mode 100644 index 00000000000..385e5055d16 --- /dev/null +++ b/test/EFCore.Relational.Specification.Tests/DesignTimeTestBase.cs @@ -0,0 +1,68 @@ +// 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 System.Reflection; +using Microsoft.EntityFrameworkCore.Design; +using Microsoft.EntityFrameworkCore.Migrations.Design; +using Microsoft.EntityFrameworkCore.Scaffolding; +using Microsoft.EntityFrameworkCore.TestUtilities; +using Microsoft.Extensions.DependencyInjection; +using Xunit; + +namespace Microsoft.EntityFrameworkCore +{ + public abstract class DesignTimeTestBase : IClassFixture + where TFixture : DesignTimeTestBase.DesignTimeFixtureBase + { + protected TFixture Fixture { get; } + + protected DesignTimeTestBase(TFixture fixture) + => Fixture = fixture; + + protected abstract Assembly ProviderAssembly { get; } + + [ConditionalFact] + public void Can_get_reverse_engineering_services() + { + using var context = Fixture.CreateContext(); + var serviceCollection = new ServiceCollection() + .AddEntityFrameworkDesignTimeServices(); + ((IDesignTimeServices)Activator.CreateInstance( + ProviderAssembly.GetType( + ProviderAssembly.GetCustomAttribute().TypeName, + throwOnError: true))!) + .ConfigureDesignTimeServices(serviceCollection); + using var services = serviceCollection.BuildServiceProvider(); + + var reverseEngineerScaffolder = services.GetService(); + + Assert.NotNull(reverseEngineerScaffolder); + } + + [ConditionalFact] + public void Can_get_migrations_services() + { + using var context = Fixture.CreateContext(); + var serviceCollection = new ServiceCollection() + .AddEntityFrameworkDesignTimeServices() + .AddDbContextDesignTimeServices(context); + ((IDesignTimeServices)Activator.CreateInstance( + ProviderAssembly.GetType( + ProviderAssembly.GetCustomAttribute().TypeName, + throwOnError: true))!) + .ConfigureDesignTimeServices(serviceCollection); + using var services = serviceCollection.BuildServiceProvider(); + + var migrationsScaffolder = services.GetService(); + + Assert.NotNull(migrationsScaffolder); + } + + public abstract class DesignTimeFixtureBase : SharedStoreFixtureBase + { + protected override string StoreName + => "DesignTimeTest"; + } + } +} diff --git a/test/EFCore.Relational.Specification.Tests/EFCore.Relational.Specification.Tests.csproj b/test/EFCore.Relational.Specification.Tests/EFCore.Relational.Specification.Tests.csproj index 4d3d35d18ca..b7f01551b83 100644 --- a/test/EFCore.Relational.Specification.Tests/EFCore.Relational.Specification.Tests.csproj +++ b/test/EFCore.Relational.Specification.Tests/EFCore.Relational.Specification.Tests.csproj @@ -15,6 +15,7 @@ + diff --git a/test/EFCore.SqlServer.FunctionalTests/DesignTimeSqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/DesignTimeSqlServerTest.cs new file mode 100644 index 00000000000..8595d673fe8 --- /dev/null +++ b/test/EFCore.SqlServer.FunctionalTests/DesignTimeSqlServerTest.cs @@ -0,0 +1,26 @@ +// 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.Reflection; +using Microsoft.EntityFrameworkCore.SqlServer.Design.Internal; +using Microsoft.EntityFrameworkCore.TestUtilities; + +namespace Microsoft.EntityFrameworkCore +{ + public class DesignTimeSqlServerTest : DesignTimeTestBase + { + public DesignTimeSqlServerTest(DesignTimeSqlServerFixture fixture) + : base(fixture) + { + } + + protected override Assembly ProviderAssembly + => typeof(SqlServerDesignTimeServices).Assembly; + + public class DesignTimeSqlServerFixture : DesignTimeFixtureBase + { + protected override ITestStoreFactory TestStoreFactory + => SqlServerTestStoreFactory.Instance; + } + } +} diff --git a/test/EFCore.SqlServer.FunctionalTests/EFCore.SqlServer.FunctionalTests.csproj b/test/EFCore.SqlServer.FunctionalTests/EFCore.SqlServer.FunctionalTests.csproj index 5e5fa498dd8..9ffc5efd390 100644 --- a/test/EFCore.SqlServer.FunctionalTests/EFCore.SqlServer.FunctionalTests.csproj +++ b/test/EFCore.SqlServer.FunctionalTests/EFCore.SqlServer.FunctionalTests.csproj @@ -23,7 +23,6 @@ - diff --git a/test/EFCore.Sqlite.FunctionalTests/DesignTimeSqliteTest.cs b/test/EFCore.Sqlite.FunctionalTests/DesignTimeSqliteTest.cs new file mode 100644 index 00000000000..8eb6cafe370 --- /dev/null +++ b/test/EFCore.Sqlite.FunctionalTests/DesignTimeSqliteTest.cs @@ -0,0 +1,26 @@ +// 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.Reflection; +using Microsoft.EntityFrameworkCore.Sqlite.Design.Internal; +using Microsoft.EntityFrameworkCore.TestUtilities; + +namespace Microsoft.EntityFrameworkCore +{ + public class DesignTimeSqliteTest : DesignTimeTestBase + { + public DesignTimeSqliteTest(DesignTimeSqliteFixture fixture) + : base(fixture) + { + } + + protected override Assembly ProviderAssembly + => typeof(SqliteDesignTimeServices).Assembly; + + public class DesignTimeSqliteFixture : DesignTimeFixtureBase + { + protected override ITestStoreFactory TestStoreFactory + => SqliteTestStoreFactory.Instance; + } + } +}