From 5fba08562fa9151c45ea4f4d51a86f232d56dfc7 Mon Sep 17 00:00:00 2001 From: Arthur Vickers Date: Thu, 10 Mar 2016 12:57:49 -0800 Subject: [PATCH] Update IdentityDbContext constructors to reflect EF changes EF now has a parameterless constructor that can only be used when a derived context has OnConfiguring and is hence protected, and a constructor that takes a DbContextOptions. --- samples/IdentitySample.Mvc/Startup.cs | 4 +- .../IdentityDbContext.cs | 51 ++----------------- .../InMemoryContext.cs | 8 ++- .../InMemoryEFUserStoreTest.cs | 4 +- .../RoleStoreTest.cs | 8 +-- .../TestIdentityFactory.cs | 6 ++- .../CustomPocoTest.cs | 4 ++ .../DbUtil.cs | 4 +- .../DefaultPocoTest.cs | 2 - .../UserStoreTest.cs | 12 +++-- 10 files changed, 38 insertions(+), 65 deletions(-) diff --git a/samples/IdentitySample.Mvc/Startup.cs b/samples/IdentitySample.Mvc/Startup.cs index 7a4a74d08a..2cb9b9a7dc 100644 --- a/samples/IdentitySample.Mvc/Startup.cs +++ b/samples/IdentitySample.Mvc/Startup.cs @@ -42,9 +42,7 @@ namespace IdentitySample public void ConfigureServices(IServiceCollection services) { // Add framework services. - services.AddEntityFramework() - .AddSqlServer() - .AddDbContext(options => + services.AddDbContext(options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])); services.AddIdentity(options => { diff --git a/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/IdentityDbContext.cs b/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/IdentityDbContext.cs index a9eae3ced3..fc063ec062 100644 --- a/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/IdentityDbContext.cs +++ b/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/IdentityDbContext.cs @@ -19,25 +19,10 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore public IdentityDbContext(DbContextOptions options) : base(options) { } - /// - /// Initializes a new instance of the class using an . - /// - /// The service provider to be used. - public IdentityDbContext(IServiceProvider serviceProvider) : base(serviceProvider) - { } - - /// - /// Initializes a new instance of the class using an . - /// - /// The options to be used by a . - /// The service provider to be used. - public IdentityDbContext(IServiceProvider serviceProvider, DbContextOptions options) : base(serviceProvider, options) - { } - /// /// Initializes a new instance of the class. /// - public IdentityDbContext() + protected IdentityDbContext() { } } @@ -54,25 +39,10 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore public IdentityDbContext(DbContextOptions options) : base(options) { } - /// - /// Initializes a new instance of the class using an . - /// - /// The service provider to be used. - public IdentityDbContext(IServiceProvider serviceProvider) : base(serviceProvider) - { } - - /// - /// Initializes a new instance of the class using an . - /// - /// The options to be used by a . - /// The service provider to be used. - public IdentityDbContext(IServiceProvider serviceProvider, DbContextOptions options) : base(serviceProvider, options) - { } - /// /// Initializes a new instance of the class. /// - public IdentityDbContext() + protected IdentityDbContext() { } } @@ -94,25 +64,10 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore public IdentityDbContext(DbContextOptions options) : base(options) { } - /// - /// Initializes a new instance of the class using an . - /// - /// The service provider to be used. - public IdentityDbContext(IServiceProvider serviceProvider) : base(serviceProvider) - { } - - /// - /// Initializes a new instance of the class using an . - /// - /// The options to be used by a . - /// The service provider to be used. - public IdentityDbContext(IServiceProvider serviceProvider, DbContextOptions options) : base(serviceProvider, options) - { } - /// /// Initializes a new instance of the class. /// - public IdentityDbContext() + protected IdentityDbContext() { } /// diff --git a/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test/InMemoryContext.cs b/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test/InMemoryContext.cs index 61c0a9ac80..36b8e30152 100644 --- a/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test/InMemoryContext.cs +++ b/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test/InMemoryContext.cs @@ -3,18 +3,23 @@ using System; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test { public class InMemoryContext : InMemoryContext { + public InMemoryContext(DbContextOptions options) : base(options) + { } } public class InMemoryContext : InMemoryContext where TUser : IdentityUser { + public InMemoryContext(DbContextOptions options) : base(options) + { } } public class InMemoryContext : IdentityDbContext @@ -22,7 +27,8 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test where TRole : IdentityRole where TKey : IEquatable { - public InMemoryContext() { } + public InMemoryContext(DbContextOptions options) : base(options) + { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { diff --git a/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test/InMemoryEFUserStoreTest.cs b/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test/InMemoryEFUserStoreTest.cs index 48f6d48a0c..3a373cadef 100644 --- a/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test/InMemoryEFUserStoreTest.cs +++ b/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test/InMemoryEFUserStoreTest.cs @@ -4,6 +4,8 @@ using System; using System.Linq.Expressions; using Microsoft.AspNetCore.Identity.Test; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test @@ -12,7 +14,7 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test { protected override object CreateTestContext() { - return new InMemoryContext(); + return new InMemoryContext(new DbContextOptionsBuilder().Options); } protected override void AddUserStore(IServiceCollection services, object context = null) diff --git a/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test/RoleStoreTest.cs b/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test/RoleStoreTest.cs index 089fc91931..a8c236d590 100644 --- a/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test/RoleStoreTest.cs +++ b/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test/RoleStoreTest.cs @@ -4,6 +4,8 @@ using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Identity.Test; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.Extensions.DependencyInjection; using Xunit; @@ -23,7 +25,7 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test public async Task CanCreateRoleWithSingletonManager() { var services = TestIdentityFactory.CreateTestServices(); - services.AddEntityFramework().AddInMemoryDatabase(); + services.AddAddEntityFrameworkInMemoryDatabase(); services.AddTransient(); services.AddTransient, RoleStore>(); services.AddSingleton>(); @@ -36,7 +38,7 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test [Fact] public async Task RoleStoreMethodsThrowWhenDisposedTest() { - var store = new RoleStore(new InMemoryContext()); + var store = new RoleStore(new InMemoryContext(new DbContextOptionsBuilder().Options)); store.Dispose(); await Assert.ThrowsAsync(async () => await store.FindByIdAsync(null)); await Assert.ThrowsAsync(async () => await store.FindByNameAsync(null)); @@ -52,7 +54,7 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test public async Task RoleStorePublicNullCheckTest() { Assert.Throws("context", () => new RoleStore(null)); - var store = new RoleStore(new InMemoryContext()); + var store = new RoleStore(new InMemoryContext(new DbContextOptionsBuilder().Options)); await Assert.ThrowsAsync("role", async () => await store.GetRoleIdAsync(null)); await Assert.ThrowsAsync("role", async () => await store.GetRoleNameAsync(null)); await Assert.ThrowsAsync("role", async () => await store.SetRoleNameAsync(null, null)); diff --git a/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test/TestIdentityFactory.cs b/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test/TestIdentityFactory.cs index df802c32b1..d8729b4a7b 100644 --- a/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test/TestIdentityFactory.cs +++ b/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test/TestIdentityFactory.cs @@ -3,6 +3,8 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Internal; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test @@ -12,10 +14,10 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test public static InMemoryContext CreateContext() { var services = new ServiceCollection(); - services.AddEntityFramework().AddInMemoryDatabase(); + services.AddAddEntityFrameworkInMemoryDatabase(); var serviceProvider = services.BuildServiceProvider(); - var db = new InMemoryContext(); + var db = new InMemoryContext(new DbContextOptionsBuilder().Options); db.Database.EnsureCreated(); return db; diff --git a/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test/CustomPocoTest.cs b/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test/CustomPocoTest.cs index c5476179fd..e2886d9174 100644 --- a/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test/CustomPocoTest.cs +++ b/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test/CustomPocoTest.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Testing.xunit; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; using Xunit; namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test @@ -27,6 +28,9 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test public class CustomDbContext : DbContext where TKey : IEquatable { + public CustomDbContext(DbContextOptions options) : base(options) + { } + public DbSet> Users { get; set; } } diff --git a/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test/DbUtil.cs b/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test/DbUtil.cs index 4708f22d39..1c4cc3ba8a 100644 --- a/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test/DbUtil.cs +++ b/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test/DbUtil.cs @@ -23,11 +23,11 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test services = new ServiceCollection(); } services.AddSingleton(); - services.AddEntityFramework().AddSqlServer().AddDbContext(options => options.UseSqlServer(connectionString)); + services.AddDbContext(options => options.UseSqlServer(connectionString)); return services; } - public static TContext Create(string connectionString) where TContext : DbContext, new() + public static TContext Create(string connectionString) where TContext : DbContext { var serviceProvider = ConfigureDbServices(connectionString).BuildServiceProvider(); return serviceProvider.GetRequiredService(); diff --git a/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test/DefaultPocoTest.cs b/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test/DefaultPocoTest.cs index 25c2b84028..e2da4712fb 100644 --- a/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test/DefaultPocoTest.cs +++ b/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test/DefaultPocoTest.cs @@ -24,8 +24,6 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test var services = new ServiceCollection(); services - .AddEntityFramework() - .AddSqlServer() .AddDbContext(o => o.UseSqlServer(fixture.ConnectionString)) .ServiceCollection() .AddIdentity() diff --git a/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test/UserStoreTest.cs b/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test/UserStoreTest.cs index adcfe3cac8..798e8c9d27 100644 --- a/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test/UserStoreTest.cs +++ b/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test/UserStoreTest.cs @@ -8,6 +8,8 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Identity.Test; using Microsoft.AspNetCore.Testing.xunit; using Microsoft.AspNetCore.Testing; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.Extensions.DependencyInjection; using Xunit; @@ -25,7 +27,11 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test protected override bool ShouldSkipDbTests() => TestPlatformHelper.IsMono || !TestPlatformHelper.IsWindows; - public class ApplicationDbContext : IdentityDbContext { } + public class ApplicationDbContext : IdentityDbContext + { + public ApplicationDbContext(DbContextOptions options) : base(options) + { } + } [ConditionalFact] [FrameworkSkipCondition(RuntimeFrameworks.Mono)] @@ -84,7 +90,7 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test [Fact] public async Task SqlUserStoreMethodsThrowWhenDisposedTest() { - var store = new UserStore(new IdentityDbContext()); + var store = new UserStore(new IdentityDbContext(new DbContextOptionsBuilder().Options)); store.Dispose(); await Assert.ThrowsAsync(async () => await store.AddClaimsAsync(null, null)); await Assert.ThrowsAsync(async () => await store.AddLoginAsync(null, null)); @@ -118,7 +124,7 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test public async Task UserStorePublicNullCheckTest() { Assert.Throws("context", () => new UserStore(null)); - var store = new UserStore(new IdentityDbContext()); + var store = new UserStore(new IdentityDbContext(new DbContextOptionsBuilder().Options)); await Assert.ThrowsAsync("user", async () => await store.GetUserIdAsync(null)); await Assert.ThrowsAsync("user", async () => await store.GetUserNameAsync(null)); await Assert.ThrowsAsync("user", async () => await store.SetUserNameAsync(null, null));