From 3a9036a69fa5c46422b00a2ea8901982d6c7ebb3 Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Thu, 16 Oct 2014 15:14:46 -0700 Subject: [PATCH] Rationalize IdentityDbContext ctors Also clean up unit tests --- .../Models/IdentityModels.cs | 8 +-- samples/IdentitySample.Mvc/Startup.cs | 8 +-- .../IdentityDbContext.cs | 19 ++----- ...ityFrameworkServiceCollectionExtensions.cs | 21 ++++---- .../InMemoryEFUserStoreTest.cs | 2 +- .../CustomPocoTest.cs | 8 +-- .../DbUtil.cs | 48 ++++++++++++++++++ .../DefaultPocoTest.cs | 11 ++-- .../SqlStoreTestBase.cs | 33 ++++-------- .../UserStoreGuidKeyTest.cs | 16 +++--- .../UserStoreIntKeyTest.cs | 5 -- .../UserStoreTest.cs | 50 +++++-------------- .../project.json | 1 + test/Shared/ApplicationUser.cs | 7 +++ 14 files changed, 109 insertions(+), 128 deletions(-) create mode 100644 test/Microsoft.AspNet.Identity.EntityFramework.Test/DbUtil.cs create mode 100644 test/Shared/ApplicationUser.cs diff --git a/samples/IdentitySample.Mvc/Models/IdentityModels.cs b/samples/IdentitySample.Mvc/Models/IdentityModels.cs index aaf0d2688f..5f867152a7 100644 --- a/samples/IdentitySample.Mvc/Models/IdentityModels.cs +++ b/samples/IdentitySample.Mvc/Models/IdentityModels.cs @@ -8,13 +8,7 @@ namespace IdentitySample.Models { public class ApplicationUser : IdentityUser { } - public class ApplicationDbContext : IdentityDbContext - { - public ApplicationDbContext(IServiceProvider serviceProvider, IOptions optionsAccessor) - : base(serviceProvider, optionsAccessor.Options) - { - } - } + public class ApplicationDbContext : IdentityDbContext { } public class IdentityDbContextOptions : DbContextOptions { diff --git a/samples/IdentitySample.Mvc/Startup.cs b/samples/IdentitySample.Mvc/Startup.cs index 56abca8eb5..e77d6b8367 100644 --- a/samples/IdentitySample.Mvc/Startup.cs +++ b/samples/IdentitySample.Mvc/Startup.cs @@ -1,3 +1,4 @@ +using System; using IdentitySample.Models; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Diagnostics; @@ -6,7 +7,6 @@ using Microsoft.AspNet.Routing; using Microsoft.Data.Entity; using Microsoft.Framework.ConfigurationModel; using Microsoft.Framework.DependencyInjection; -using System; namespace IdentitySamples { @@ -27,13 +27,13 @@ namespace IdentitySamples public void ConfigureServices(IServiceCollection services) { - services.AddEntityFramework().AddSqlServer(); - services.AddScoped(); + services.AddEntityFramework() + .AddSqlServer() + .AddDbContext(options => options.UseSqlServer(Configuration.Get("Data:IdentityConnection:ConnectionString"))); services.Configure(options => { options.DefaultAdminUserName = Configuration.Get("DefaultAdminUsername"); options.DefaultAdminPassword = Configuration.Get("DefaultAdminPassword"); - options.UseSqlServer(Configuration.Get("Data:IdentityConnection:ConnectionString")); }); services.AddDefaultIdentity(Configuration, options => diff --git a/src/Microsoft.AspNet.Identity.EntityFramework/IdentityDbContext.cs b/src/Microsoft.AspNet.Identity.EntityFramework/IdentityDbContext.cs index 3ee240649f..add648bd0b 100644 --- a/src/Microsoft.AspNet.Identity.EntityFramework/IdentityDbContext.cs +++ b/src/Microsoft.AspNet.Identity.EntityFramework/IdentityDbContext.cs @@ -7,20 +7,10 @@ using Microsoft.Data.Entity.Metadata; namespace Microsoft.AspNet.Identity.EntityFramework { - public class IdentityDbContext : - IdentityDbContext - { - public IdentityDbContext() { } - public IdentityDbContext(IServiceProvider serviceProvider, DbContextOptions options) : base(serviceProvider, options) { } - } + public class IdentityDbContext : IdentityDbContext { } - public class IdentityDbContext : - IdentityDbContext - where TUser : IdentityUser - { - public IdentityDbContext() { } - public IdentityDbContext(IServiceProvider serviceProvider, DbContextOptions options) : base(serviceProvider, options) { } - } + public class IdentityDbContext : IdentityDbContext where TUser : IdentityUser + { } public class IdentityDbContext : DbContext where TUser : IdentityUser @@ -34,9 +24,6 @@ namespace Microsoft.AspNet.Identity.EntityFramework public DbSet Roles { get; set; } public DbSet> RoleClaims { get; set; } - public IdentityDbContext() { } - public IdentityDbContext(IServiceProvider serviceProvider, DbContextOptions options) : base(serviceProvider, options) { } - protected override void OnModelCreating(ModelBuilder builder) { builder.Entity(b => diff --git a/src/Microsoft.AspNet.Identity.EntityFramework/IdentityEntityFrameworkServiceCollectionExtensions.cs b/src/Microsoft.AspNet.Identity.EntityFramework/IdentityEntityFrameworkServiceCollectionExtensions.cs index 0d8a6746b9..fe15737b48 100644 --- a/src/Microsoft.AspNet.Identity.EntityFramework/IdentityEntityFrameworkServiceCollectionExtensions.cs +++ b/src/Microsoft.AspNet.Identity.EntityFramework/IdentityEntityFrameworkServiceCollectionExtensions.cs @@ -1,27 +1,26 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using Microsoft.AspNet.Builder; +using System; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.EntityFramework; using Microsoft.Data.Entity; using Microsoft.Framework.ConfigurationModel; -using System; namespace Microsoft.Framework.DependencyInjection { public static class IdentityEntityFrameworkServiceCollectionExtensions { // MOVE to builder extension - public static IdentityBuilder AddIdentitySqlServer(this IServiceCollection services) + public static IdentityBuilder AddIdentityEntityFramework(this IServiceCollection services) { - return services.AddIdentitySqlServer(); + return services.AddIdentityEntityFramework(); } - public static IdentityBuilder AddIdentitySqlServer(this IServiceCollection services) + public static IdentityBuilder AddIdentityEntityFramework(this IServiceCollection services) where TContext : DbContext { - return services.AddIdentitySqlServer(); + return services.AddIdentityEntityFramework(); } public static IdentityBuilder AddDefaultIdentity(this IServiceCollection services, IConfiguration config = null, @@ -34,14 +33,14 @@ namespace Microsoft.Framework.DependencyInjection .AddEntityFramework(); } - public static IdentityBuilder AddIdentitySqlServer(this IServiceCollection services, Action configureOptions = null) + public static IdentityBuilder AddIdentityEntityFramework(this IServiceCollection services, Action configureOptions = null) where TUser : IdentityUser, new() where TContext : DbContext { - return services.AddIdentitySqlServer(null, configureOptions); + return services.AddIdentityEntityFramework(null, configureOptions); } - public static IdentityBuilder AddIdentitySqlServer(this IServiceCollection services, IConfiguration config = null, Action configureOptions = null) + public static IdentityBuilder AddIdentityEntityFramework(this IServiceCollection services, IConfiguration config = null, Action configureOptions = null) where TUser : IdentityUser, new() where TRole : IdentityRole, new() where TContext : DbContext @@ -49,11 +48,10 @@ namespace Microsoft.Framework.DependencyInjection var builder = services.AddIdentity(config, configureOptions); services.AddScoped, UserStore>(); services.AddScoped, RoleStore>(); - services.AddScoped(); return builder; } - public static IdentityBuilder AddIdentitySqlServer(this IServiceCollection services, IConfiguration config = null, Action configureOptions = null) + public static IdentityBuilder AddIdentityEntityFramework(this IServiceCollection services, IConfiguration config = null, Action configureOptions = null) where TUser : IdentityUser, new() where TRole : IdentityRole, new() where TContext : DbContext @@ -62,7 +60,6 @@ namespace Microsoft.Framework.DependencyInjection var builder = services.AddIdentity(config, configureOptions); services.AddScoped, UserStore>(); services.AddScoped, RoleStore>(); - services.AddScoped(); return builder; } } diff --git a/test/Microsoft.AspNet.Identity.EntityFramework.InMemory.Test/InMemoryEFUserStoreTest.cs b/test/Microsoft.AspNet.Identity.EntityFramework.InMemory.Test/InMemoryEFUserStoreTest.cs index 653d8bbfab..abc427d180 100644 --- a/test/Microsoft.AspNet.Identity.EntityFramework.InMemory.Test/InMemoryEFUserStoreTest.cs +++ b/test/Microsoft.AspNet.Identity.EntityFramework.InMemory.Test/InMemoryEFUserStoreTest.cs @@ -8,7 +8,7 @@ using Microsoft.Framework.OptionsModel; namespace Microsoft.AspNet.Identity.EntityFramework.InMemory.Test { - public class InMemoryEFUserStoreTest : UserManagerTestBase + public class InMemoryEFUserStoreTest : UserManagerTestBase { protected override object CreateTestContext() { diff --git a/test/Microsoft.AspNet.Identity.EntityFramework.Test/CustomPocoTest.cs b/test/Microsoft.AspNet.Identity.EntityFramework.Test/CustomPocoTest.cs index add81005cd..3efdaaf2ac 100644 --- a/test/Microsoft.AspNet.Identity.EntityFramework.Test/CustomPocoTest.cs +++ b/test/Microsoft.AspNet.Identity.EntityFramework.Test/CustomPocoTest.cs @@ -32,10 +32,6 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test { public DbSet> Users { get; set; } - public CustomDbContext(IServiceProvider services) : - base(services, services.GetService>().Options) - { } - protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); @@ -45,11 +41,9 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test } } - public CustomDbContext GetContext() where TKey : IEquatable { - var serviceProvider = UserStoreTest.ConfigureDbServices(ConnectionString).BuildServiceProvider(); - return new CustomDbContext(serviceProvider); + return DbUtil.Create>(ConnectionString); } public CustomDbContext CreateContext(bool delete = false) where TKey : IEquatable diff --git a/test/Microsoft.AspNet.Identity.EntityFramework.Test/DbUtil.cs b/test/Microsoft.AspNet.Identity.EntityFramework.Test/DbUtil.cs new file mode 100644 index 0000000000..fccea8f43b --- /dev/null +++ b/test/Microsoft.AspNet.Identity.EntityFramework.Test/DbUtil.cs @@ -0,0 +1,48 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNet.Hosting; +using Microsoft.Data.Entity; +using Microsoft.Data.Entity.Services; +using Microsoft.Framework.DependencyInjection; +using Microsoft.Framework.DependencyInjection.Fallback; +using Microsoft.Framework.Logging; +using Microsoft.Framework.OptionsModel; +using Xunit; + +namespace Microsoft.AspNet.Identity.EntityFramework.Test +{ + [TestCaseOrderer("Microsoft.AspNet.Identity.Test.PriorityOrderer", "Microsoft.AspNet.Identity.EntityFramework.Test")] + public static class DbUtil + { + public static IServiceCollection ConfigureDbServices(string connectionString, IServiceCollection services = null) + { + return ConfigureDbServices(connectionString, services); + } + + public static IServiceCollection ConfigureDbServices(string connectionString, IServiceCollection services = null) where TContext : DbContext + { + if (services == null) + { + services = new ServiceCollection(); + } + services.Add(HostingServices.GetDefaultServices()); + services.AddEntityFramework().AddSqlServer().AddDbContext(options => options.UseSqlServer(connectionString)); + services.Add(OptionsServices.GetDefaultServices()); + services.AddInstance(new NullLoggerFactory()); + return services; + } + + public static IdentityDbContext Create(string connectionString) + { + return Create(connectionString); + } + + public static TContext Create(string connectionString) where TContext : DbContext, new() + { + var serviceProvider = ConfigureDbServices(connectionString).BuildServiceProvider(); + return serviceProvider.GetService(); + } + + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Identity.EntityFramework.Test/DefaultPocoTest.cs b/test/Microsoft.AspNet.Identity.EntityFramework.Test/DefaultPocoTest.cs index d96d0c2351..9cc9c11541 100644 --- a/test/Microsoft.AspNet.Identity.EntityFramework.Test/DefaultPocoTest.cs +++ b/test/Microsoft.AspNet.Identity.EntityFramework.Test/DefaultPocoTest.cs @@ -21,10 +21,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test private readonly string ConnectionString = @"Server=(localdb)\v11.0;Database=DefaultSchemaTest" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year + ";Trusted_Connection=True;"; public IdentityDbContext CreateContext(bool ensureCreated = false) { - var services = UserStoreTest.ConfigureDbServices(ConnectionString); - var serviceProvider = services.BuildServiceProvider(); - var db = new IdentityDbContext(serviceProvider, - serviceProvider.GetService>().Options); + var db = DbUtil.Create(ConnectionString); if (ensureCreated) { db.Database.EnsureCreated(); @@ -53,10 +50,8 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test builder.UseServices(services => { - UserStoreTest.ConfigureDbServices(ConnectionString, services); - services.AddIdentitySqlServer(); - // todo: constructor resolution doesn't work well with IdentityDbContext since it has 4 constructors - services.AddInstance(context); + DbUtil.ConfigureDbServices(ConnectionString, services); + services.AddDefaultIdentity(); }); var userStore = builder.ApplicationServices.GetService>(); diff --git a/test/Microsoft.AspNet.Identity.EntityFramework.Test/SqlStoreTestBase.cs b/test/Microsoft.AspNet.Identity.EntityFramework.Test/SqlStoreTestBase.cs index a17fe8af20..4c9655dd48 100644 --- a/test/Microsoft.AspNet.Identity.EntityFramework.Test/SqlStoreTestBase.cs +++ b/test/Microsoft.AspNet.Identity.EntityFramework.Test/SqlStoreTestBase.cs @@ -7,12 +7,8 @@ using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Identity.Test; -using Microsoft.Data.Entity; -using Microsoft.Data.Entity.Services; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection.Fallback; -using Microsoft.Framework.Logging; -using Microsoft.Framework.OptionsModel; using Xunit; namespace Microsoft.AspNet.Identity.EntityFramework.Test @@ -25,10 +21,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test { public abstract string ConnectionString { get; } - public class ApplicationDbContext : IdentityDbContext - { - public ApplicationDbContext(IServiceProvider services, IOptions options) : base(services, options.Options) { } - } + public class TestDbContext : IdentityDbContext { } [TestPriority(-1000)] [Fact] @@ -46,17 +39,13 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test public void DropDb() { - var serviceProvider = UserStoreTest.ConfigureDbServices(ConnectionString).BuildServiceProvider(); - var db = new ApplicationDbContext(serviceProvider, - serviceProvider.GetService>()); + var db = DbUtil.Create(ConnectionString); db.Database.EnsureDeleted(); } - public ApplicationDbContext CreateContext(bool delete = false) + public TestDbContext CreateContext(bool delete = false) { - var serviceProvider = UserStoreTest.ConfigureDbServices(ConnectionString).BuildServiceProvider(); - var db = new ApplicationDbContext(serviceProvider, - serviceProvider.GetService>()); + var db = DbUtil.Create(ConnectionString); if (delete) { db.Database.EnsureDeleted(); @@ -76,17 +65,17 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test { context = CreateTestContext(); } - return MockHelpers.CreateManager(new UserStore((ApplicationDbContext)context)); + return MockHelpers.CreateManager(new UserStore((TestDbContext)context)); } protected override RoleManager CreateRoleManager(object context = null) { + var services = DbUtil.ConfigureDbServices(ConnectionString); if (context == null) { context = CreateTestContext(); } - var services = UserStoreTest.ConfigureDbServices(ConnectionString); - services.AddIdentity().AddRoleStore(new RoleStore((ApplicationDbContext)context)); + services.AddIdentity().AddRoleStore(new RoleStore((TestDbContext)context)); return services.BuildServiceProvider().GetService>(); } @@ -103,8 +92,8 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test builder.UseServices(services => { - UserStoreTest.ConfigureDbServices(ConnectionString, services); - services.AddIdentitySqlServer(); + DbUtil.ConfigureDbServices(ConnectionString, services); + services.AddIdentityEntityFramework(); }); var userStore = builder.ApplicationServices.GetService>(); @@ -128,8 +117,8 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test builder.UseServices(services => { - UserStoreTest.ConfigureDbServices(ConnectionString, services); - services.AddIdentitySqlServer(); + DbUtil.ConfigureDbServices(ConnectionString, services); + services.AddIdentityEntityFramework(); services.ConfigureIdentity(options => { options.Password.RequiredLength = 1; diff --git a/test/Microsoft.AspNet.Identity.EntityFramework.Test/UserStoreGuidKeyTest.cs b/test/Microsoft.AspNet.Identity.EntityFramework.Test/UserStoreGuidKeyTest.cs index db3dd1f2e5..a3469c34ec 100644 --- a/test/Microsoft.AspNet.Identity.EntityFramework.Test/UserStoreGuidKeyTest.cs +++ b/test/Microsoft.AspNet.Identity.EntityFramework.Test/UserStoreGuidKeyTest.cs @@ -3,10 +3,8 @@ using System; using Microsoft.AspNet.Identity.Test; -using Microsoft.Data.Entity.Services; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection.Fallback; -using Microsoft.Framework.Logging; using Xunit; namespace Microsoft.AspNet.Identity.EntityFramework.Test @@ -42,9 +40,9 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test } } - public class ApplicationUserStore : UserStore + public class ApplicationUserStore : UserStore { - public ApplicationUserStore(ApplicationDbContext context) : base(context) { } + public ApplicationUserStore(TestDbContext context) : base(context) { } public override Guid ConvertIdFromString(string userId) { @@ -52,9 +50,9 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test } } - public class ApplicationRoleStore : RoleStore + public class ApplicationRoleStore : RoleStore { - public ApplicationRoleStore(ApplicationDbContext context) : base(context) { } + public ApplicationRoleStore(TestDbContext context) : base(context) { } public override Guid ConvertIdFromString(string id) { @@ -68,7 +66,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test { context = CreateTestContext(); } - return MockHelpers.CreateManager(new ApplicationUserStore((ApplicationDbContext)context)); + return MockHelpers.CreateManager(new ApplicationUserStore((TestDbContext)context)); } protected override RoleManager CreateRoleManager(object context) @@ -77,8 +75,8 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test { context = CreateTestContext(); } - var services = UserStoreTest.ConfigureDbServices(ConnectionString); - services.AddIdentity().AddRoleStore(new ApplicationRoleStore((ApplicationDbContext)context)); + var services = DbUtil.ConfigureDbServices(ConnectionString); + services.AddIdentity().AddRoleStore(new ApplicationRoleStore((TestDbContext)context)); return services.BuildServiceProvider().GetService>(); } } diff --git a/test/Microsoft.AspNet.Identity.EntityFramework.Test/UserStoreIntKeyTest.cs b/test/Microsoft.AspNet.Identity.EntityFramework.Test/UserStoreIntKeyTest.cs index 8873484182..3fee6e3c71 100644 --- a/test/Microsoft.AspNet.Identity.EntityFramework.Test/UserStoreIntKeyTest.cs +++ b/test/Microsoft.AspNet.Identity.EntityFramework.Test/UserStoreIntKeyTest.cs @@ -1,11 +1,6 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using Microsoft.AspNet.Identity.Test; -using Microsoft.Data.Entity; -using Microsoft.Framework.DependencyInjection; -using Microsoft.Framework.DependencyInjection.Fallback; -using Microsoft.Framework.OptionsModel; using System; using Xunit; diff --git a/test/Microsoft.AspNet.Identity.EntityFramework.Test/UserStoreTest.cs b/test/Microsoft.AspNet.Identity.EntityFramework.Test/UserStoreTest.cs index a0ba258745..e2f94908b2 100644 --- a/test/Microsoft.AspNet.Identity.EntityFramework.Test/UserStoreTest.cs +++ b/test/Microsoft.AspNet.Identity.EntityFramework.Test/UserStoreTest.cs @@ -6,29 +6,24 @@ using System.Linq; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Identity.Test; using Microsoft.Data.Entity; using Microsoft.Data.Entity.Services; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection.Fallback; using Microsoft.Framework.Logging; -using Microsoft.Framework.OptionsModel; using Xunit; namespace Microsoft.AspNet.Identity.EntityFramework.Test { + public class ApplicationDbContext : IdentityDbContext { } + [TestCaseOrderer("Microsoft.AspNet.Identity.Test.PriorityOrderer", "Microsoft.AspNet.Identity.EntityFramework.Test")] public class UserStoreTest : UserManagerTestBase { private readonly string ConnectionString = @"Server=(localdb)\v11.0;Database=SqlUserStoreTest" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year + ";Trusted_Connection=True;"; - public class ApplicationUser : IdentityUser { } - - public class ApplicationDbContext : IdentityDbContext - { - public ApplicationDbContext(IServiceProvider services, IOptions options) : base(services, options.Options) { } - } - [TestPriority(-1000)] [Fact] public void DropDatabaseStart() @@ -45,9 +40,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test public void DropDb() { - var serviceProvider = ConfigureDbServices(ConnectionString).BuildServiceProvider(); - var db = new ApplicationDbContext(serviceProvider, - serviceProvider.GetService>()); + var db = DbUtil.Create(ConnectionString); db.Database.EnsureDeleted(); } @@ -59,8 +52,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test builder.UseServices(services => { - ConfigureDbServices(ConnectionString, services); - services.AddScoped(); + DbUtil.ConfigureDbServices(ConnectionString, services); services.AddDefaultIdentity(); }); @@ -86,8 +78,11 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test builder.UseServices(services => { services.AddInstance(new NullLoggerFactory()); - services.AddEntityFramework().AddSqlServer(); - services.AddIdentitySqlServer(options => + services.Add(HostingServices.GetDefaultServices()); + services.AddEntityFramework() + .AddSqlServer() + .AddDbContext(options => options.UseSqlServer(ConnectionString)); + services.AddIdentityEntityFramework(options => { options.Password.RequiredLength = 1; options.Password.RequireLowercase = false; @@ -95,8 +90,6 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test options.Password.RequireUppercase = false; options.Password.RequireDigit = false; }); - services.Configure(options => - options.UseSqlServer(ConnectionString)); }); var userStore = builder.ApplicationServices.GetService>(); @@ -127,8 +120,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test public IdentityDbContext CreateContext(bool delete = false) { - var serviceProvider = ConfigureDbServices(ConnectionString).BuildServiceProvider(); - var db = new IdentityDbContext(serviceProvider, serviceProvider.GetService>().Options); + var db = DbUtil.Create(ConnectionString); if (delete) { db.Database.EnsureDeleted(); @@ -147,25 +139,9 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test CreateContext(); } - public static IServiceCollection ConfigureDbServices(string connectionString, IServiceCollection services = null) - { - if (services == null) - { - services = new ServiceCollection(); - } - services.AddEntityFramework().AddSqlServer(); - services.Add(OptionsServices.GetDefaultServices()); - services.AddInstance(new NullLoggerFactory()); - services.Configure(options => - options.UseSqlServer(connectionString)); - return services; - } - public ApplicationDbContext CreateAppContext() { - CreateContext(); - var serviceProvider = ConfigureDbServices(ConnectionString).BuildServiceProvider(); - var db = new ApplicationDbContext(serviceProvider, serviceProvider.GetService>()); + var db = DbUtil.Create(ConnectionString); db.Database.EnsureCreated(); return db; } @@ -186,7 +162,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test public RoleManager CreateRoleManager(IdentityDbContext context) { - var services = ConfigureDbServices(ConnectionString); + var services = DbUtil.ConfigureDbServices(ConnectionString); services.AddIdentity().AddRoleStore(new RoleStore(context)); return services.BuildServiceProvider().GetService>(); } diff --git a/test/Microsoft.AspNet.Identity.EntityFramework.Test/project.json b/test/Microsoft.AspNet.Identity.EntityFramework.Test/project.json index ff3b6abc7f..b4f14912d0 100644 --- a/test/Microsoft.AspNet.Identity.EntityFramework.Test/project.json +++ b/test/Microsoft.AspNet.Identity.EntityFramework.Test/project.json @@ -1,5 +1,6 @@ { "dependencies": { + "Microsoft.AspNet.Hosting": "1.0.0-*", "Microsoft.AspNet.Http": "1.0.0-*", "Microsoft.AspNet.Identity": "3.0.0-*", "Microsoft.AspNet.Identity.EntityFramework": "1.0.0-*", diff --git a/test/Shared/ApplicationUser.cs b/test/Shared/ApplicationUser.cs new file mode 100644 index 0000000000..ea0a2afbf6 --- /dev/null +++ b/test/Shared/ApplicationUser.cs @@ -0,0 +1,7 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +namespace Microsoft.AspNet.Identity.Test +{ + public class ApplicationUser : IdentityUser { } +} \ No newline at end of file