Rationalize IdentityDbContext ctors

Also clean up unit tests
This commit is contained in:
Hao Kung 2014-10-16 15:14:46 -07:00
parent 92267b4c25
commit 3a9036a69f
14 changed files with 109 additions and 128 deletions

View File

@ -8,13 +8,7 @@ namespace IdentitySample.Models
{ {
public class ApplicationUser : IdentityUser { } public class ApplicationUser : IdentityUser { }
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { }
{
public ApplicationDbContext(IServiceProvider serviceProvider, IOptions<IdentityDbContextOptions> optionsAccessor)
: base(serviceProvider, optionsAccessor.Options)
{
}
}
public class IdentityDbContextOptions : DbContextOptions public class IdentityDbContextOptions : DbContextOptions
{ {

View File

@ -1,3 +1,4 @@
using System;
using IdentitySample.Models; using IdentitySample.Models;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Diagnostics; using Microsoft.AspNet.Diagnostics;
@ -6,7 +7,6 @@ using Microsoft.AspNet.Routing;
using Microsoft.Data.Entity; using Microsoft.Data.Entity;
using Microsoft.Framework.ConfigurationModel; using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using System;
namespace IdentitySamples namespace IdentitySamples
{ {
@ -27,13 +27,13 @@ namespace IdentitySamples
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.AddEntityFramework().AddSqlServer(); services.AddEntityFramework()
services.AddScoped<ApplicationDbContext>(); .AddSqlServer()
.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.Get("Data:IdentityConnection:ConnectionString")));
services.Configure<IdentityDbContextOptions>(options => services.Configure<IdentityDbContextOptions>(options =>
{ {
options.DefaultAdminUserName = Configuration.Get("DefaultAdminUsername"); options.DefaultAdminUserName = Configuration.Get("DefaultAdminUsername");
options.DefaultAdminPassword = Configuration.Get("DefaultAdminPassword"); options.DefaultAdminPassword = Configuration.Get("DefaultAdminPassword");
options.UseSqlServer(Configuration.Get("Data:IdentityConnection:ConnectionString"));
}); });
services.AddDefaultIdentity<ApplicationDbContext, ApplicationUser, IdentityRole>(Configuration, options => services.AddDefaultIdentity<ApplicationDbContext, ApplicationUser, IdentityRole>(Configuration, options =>

View File

@ -7,20 +7,10 @@ using Microsoft.Data.Entity.Metadata;
namespace Microsoft.AspNet.Identity.EntityFramework namespace Microsoft.AspNet.Identity.EntityFramework
{ {
public class IdentityDbContext : public class IdentityDbContext : IdentityDbContext<IdentityUser, IdentityRole, string> { }
IdentityDbContext<IdentityUser, IdentityRole, string>
{
public IdentityDbContext() { }
public IdentityDbContext(IServiceProvider serviceProvider, DbContextOptions options) : base(serviceProvider, options) { }
}
public class IdentityDbContext<TUser> : public class IdentityDbContext<TUser> : IdentityDbContext<TUser, IdentityRole, string> where TUser : IdentityUser
IdentityDbContext<TUser, IdentityRole, string> { }
where TUser : IdentityUser
{
public IdentityDbContext() { }
public IdentityDbContext(IServiceProvider serviceProvider, DbContextOptions options) : base(serviceProvider, options) { }
}
public class IdentityDbContext<TUser, TRole, TKey> : DbContext public class IdentityDbContext<TUser, TRole, TKey> : DbContext
where TUser : IdentityUser<TKey> where TUser : IdentityUser<TKey>
@ -34,9 +24,6 @@ namespace Microsoft.AspNet.Identity.EntityFramework
public DbSet<TRole> Roles { get; set; } public DbSet<TRole> Roles { get; set; }
public DbSet<IdentityRoleClaim<TKey>> RoleClaims { get; set; } public DbSet<IdentityRoleClaim<TKey>> RoleClaims { get; set; }
public IdentityDbContext() { }
public IdentityDbContext(IServiceProvider serviceProvider, DbContextOptions options) : base(serviceProvider, options) { }
protected override void OnModelCreating(ModelBuilder builder) protected override void OnModelCreating(ModelBuilder builder)
{ {
builder.Entity<TUser>(b => builder.Entity<TUser>(b =>

View File

@ -1,27 +1,26 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // 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. // 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;
using Microsoft.AspNet.Identity.EntityFramework; using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Data.Entity; using Microsoft.Data.Entity;
using Microsoft.Framework.ConfigurationModel; using Microsoft.Framework.ConfigurationModel;
using System;
namespace Microsoft.Framework.DependencyInjection namespace Microsoft.Framework.DependencyInjection
{ {
public static class IdentityEntityFrameworkServiceCollectionExtensions public static class IdentityEntityFrameworkServiceCollectionExtensions
{ {
// MOVE to builder extension // MOVE to builder extension
public static IdentityBuilder<IdentityUser, IdentityRole> AddIdentitySqlServer(this IServiceCollection services) public static IdentityBuilder<IdentityUser, IdentityRole> AddIdentityEntityFramework(this IServiceCollection services)
{ {
return services.AddIdentitySqlServer<IdentityDbContext, IdentityUser, IdentityRole>(); return services.AddIdentityEntityFramework<IdentityDbContext, IdentityUser, IdentityRole>();
} }
public static IdentityBuilder<IdentityUser, IdentityRole> AddIdentitySqlServer<TContext>(this IServiceCollection services) public static IdentityBuilder<IdentityUser, IdentityRole> AddIdentityEntityFramework<TContext>(this IServiceCollection services)
where TContext : DbContext where TContext : DbContext
{ {
return services.AddIdentitySqlServer<TContext, IdentityUser, IdentityRole>(); return services.AddIdentityEntityFramework<TContext, IdentityUser, IdentityRole>();
} }
public static IdentityBuilder<TUser, TRole> AddDefaultIdentity<TContext, TUser, TRole>(this IServiceCollection services, IConfiguration config = null, public static IdentityBuilder<TUser, TRole> AddDefaultIdentity<TContext, TUser, TRole>(this IServiceCollection services, IConfiguration config = null,
@ -34,14 +33,14 @@ namespace Microsoft.Framework.DependencyInjection
.AddEntityFramework<TContext, TUser, TRole>(); .AddEntityFramework<TContext, TUser, TRole>();
} }
public static IdentityBuilder<TUser, IdentityRole> AddIdentitySqlServer<TContext, TUser>(this IServiceCollection services, Action<IdentityOptions> configureOptions = null) public static IdentityBuilder<TUser, IdentityRole> AddIdentityEntityFramework<TContext, TUser>(this IServiceCollection services, Action<IdentityOptions> configureOptions = null)
where TUser : IdentityUser, new() where TUser : IdentityUser, new()
where TContext : DbContext where TContext : DbContext
{ {
return services.AddIdentitySqlServer<TContext, TUser, IdentityRole>(null, configureOptions); return services.AddIdentityEntityFramework<TContext, TUser, IdentityRole>(null, configureOptions);
} }
public static IdentityBuilder<TUser, TRole> AddIdentitySqlServer<TContext, TUser, TRole>(this IServiceCollection services, IConfiguration config = null, Action<IdentityOptions> configureOptions = null) public static IdentityBuilder<TUser, TRole> AddIdentityEntityFramework<TContext, TUser, TRole>(this IServiceCollection services, IConfiguration config = null, Action<IdentityOptions> configureOptions = null)
where TUser : IdentityUser, new() where TUser : IdentityUser, new()
where TRole : IdentityRole, new() where TRole : IdentityRole, new()
where TContext : DbContext where TContext : DbContext
@ -49,11 +48,10 @@ namespace Microsoft.Framework.DependencyInjection
var builder = services.AddIdentity<TUser, TRole>(config, configureOptions); var builder = services.AddIdentity<TUser, TRole>(config, configureOptions);
services.AddScoped<IUserStore<TUser>, UserStore<TUser, TRole, TContext>>(); services.AddScoped<IUserStore<TUser>, UserStore<TUser, TRole, TContext>>();
services.AddScoped<IRoleStore<TRole>, RoleStore<TRole, TContext>>(); services.AddScoped<IRoleStore<TRole>, RoleStore<TRole, TContext>>();
services.AddScoped<TContext>();
return builder; return builder;
} }
public static IdentityBuilder<TUser, TRole> AddIdentitySqlServer<TContext, TUser, TRole, TKey>(this IServiceCollection services, IConfiguration config = null, Action<IdentityOptions> configureOptions = null) public static IdentityBuilder<TUser, TRole> AddIdentityEntityFramework<TContext, TUser, TRole, TKey>(this IServiceCollection services, IConfiguration config = null, Action<IdentityOptions> configureOptions = null)
where TUser : IdentityUser<TKey>, new() where TUser : IdentityUser<TKey>, new()
where TRole : IdentityRole<TKey>, new() where TRole : IdentityRole<TKey>, new()
where TContext : DbContext where TContext : DbContext
@ -62,7 +60,6 @@ namespace Microsoft.Framework.DependencyInjection
var builder = services.AddIdentity<TUser, TRole>(config, configureOptions); var builder = services.AddIdentity<TUser, TRole>(config, configureOptions);
services.AddScoped<IUserStore<TUser>, UserStore<TUser, TRole, TContext, TKey>>(); services.AddScoped<IUserStore<TUser>, UserStore<TUser, TRole, TContext, TKey>>();
services.AddScoped<IRoleStore<TRole>, RoleStore<TRole, TContext, TKey>>(); services.AddScoped<IRoleStore<TRole>, RoleStore<TRole, TContext, TKey>>();
services.AddScoped<TContext>();
return builder; return builder;
} }
} }

View File

@ -8,7 +8,7 @@ using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Identity.EntityFramework.InMemory.Test namespace Microsoft.AspNet.Identity.EntityFramework.InMemory.Test
{ {
public class InMemoryEFUserStoreTest : UserManagerTestBase<IdentityUser, IdentityRole> public class InMemoryEFUserStoreTest : UserManagerTestBase<IdentityUser, IdentityRole>
{ {
protected override object CreateTestContext() protected override object CreateTestContext()
{ {

View File

@ -32,10 +32,6 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
{ {
public DbSet<User<TKey>> Users { get; set; } public DbSet<User<TKey>> Users { get; set; }
public CustomDbContext(IServiceProvider services) :
base(services, services.GetService<IOptions<DbContextOptions>>().Options)
{ }
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
{ {
base.OnModelCreating(modelBuilder); base.OnModelCreating(modelBuilder);
@ -45,11 +41,9 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
} }
} }
public CustomDbContext<TKey> GetContext<TKey>() where TKey : IEquatable<TKey> public CustomDbContext<TKey> GetContext<TKey>() where TKey : IEquatable<TKey>
{ {
var serviceProvider = UserStoreTest.ConfigureDbServices(ConnectionString).BuildServiceProvider(); return DbUtil.Create<CustomDbContext<TKey>>(ConnectionString);
return new CustomDbContext<TKey>(serviceProvider);
} }
public CustomDbContext<TKey> CreateContext<TKey>(bool delete = false) where TKey : IEquatable<TKey> public CustomDbContext<TKey> CreateContext<TKey>(bool delete = false) where TKey : IEquatable<TKey>

View File

@ -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<IdentityDbContext>(connectionString, services);
}
public static IServiceCollection ConfigureDbServices<TContext>(string connectionString, IServiceCollection services = null) where TContext : DbContext
{
if (services == null)
{
services = new ServiceCollection();
}
services.Add(HostingServices.GetDefaultServices());
services.AddEntityFramework().AddSqlServer().AddDbContext<TContext>(options => options.UseSqlServer(connectionString));
services.Add(OptionsServices.GetDefaultServices());
services.AddInstance<ILoggerFactory>(new NullLoggerFactory());
return services;
}
public static IdentityDbContext Create(string connectionString)
{
return Create<IdentityDbContext>(connectionString);
}
public static TContext Create<TContext>(string connectionString) where TContext : DbContext, new()
{
var serviceProvider = ConfigureDbServices<TContext>(connectionString).BuildServiceProvider();
return serviceProvider.GetService<TContext>();
}
}
}

View File

@ -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;"; 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) public IdentityDbContext CreateContext(bool ensureCreated = false)
{ {
var services = UserStoreTest.ConfigureDbServices(ConnectionString); var db = DbUtil.Create(ConnectionString);
var serviceProvider = services.BuildServiceProvider();
var db = new IdentityDbContext(serviceProvider,
serviceProvider.GetService<IOptions<DbContextOptions>>().Options);
if (ensureCreated) if (ensureCreated)
{ {
db.Database.EnsureCreated(); db.Database.EnsureCreated();
@ -53,10 +50,8 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
builder.UseServices(services => builder.UseServices(services =>
{ {
UserStoreTest.ConfigureDbServices(ConnectionString, services); DbUtil.ConfigureDbServices<IdentityDbContext>(ConnectionString, services);
services.AddIdentitySqlServer(); services.AddDefaultIdentity<IdentityDbContext, IdentityUser, IdentityRole>();
// todo: constructor resolution doesn't work well with IdentityDbContext since it has 4 constructors
services.AddInstance(context);
}); });
var userStore = builder.ApplicationServices.GetService<IUserStore<IdentityUser>>(); var userStore = builder.ApplicationServices.GetService<IUserStore<IdentityUser>>();

View File

@ -7,12 +7,8 @@ using System.Security.Claims;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Identity.Test; using Microsoft.AspNet.Identity.Test;
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Services;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.DependencyInjection.Fallback; using Microsoft.Framework.DependencyInjection.Fallback;
using Microsoft.Framework.Logging;
using Microsoft.Framework.OptionsModel;
using Xunit; using Xunit;
namespace Microsoft.AspNet.Identity.EntityFramework.Test namespace Microsoft.AspNet.Identity.EntityFramework.Test
@ -25,10 +21,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
{ {
public abstract string ConnectionString { get; } public abstract string ConnectionString { get; }
public class ApplicationDbContext : IdentityDbContext<TUser, TRole, TKey> public class TestDbContext : IdentityDbContext<TUser, TRole, TKey> { }
{
public ApplicationDbContext(IServiceProvider services, IOptions<DbContextOptions> options) : base(services, options.Options) { }
}
[TestPriority(-1000)] [TestPriority(-1000)]
[Fact] [Fact]
@ -46,17 +39,13 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
public void DropDb() public void DropDb()
{ {
var serviceProvider = UserStoreTest.ConfigureDbServices(ConnectionString).BuildServiceProvider(); var db = DbUtil.Create<TestDbContext>(ConnectionString);
var db = new ApplicationDbContext(serviceProvider,
serviceProvider.GetService<IOptions<DbContextOptions>>());
db.Database.EnsureDeleted(); db.Database.EnsureDeleted();
} }
public ApplicationDbContext CreateContext(bool delete = false) public TestDbContext CreateContext(bool delete = false)
{ {
var serviceProvider = UserStoreTest.ConfigureDbServices(ConnectionString).BuildServiceProvider(); var db = DbUtil.Create<TestDbContext>(ConnectionString);
var db = new ApplicationDbContext(serviceProvider,
serviceProvider.GetService<IOptions<DbContextOptions>>());
if (delete) if (delete)
{ {
db.Database.EnsureDeleted(); db.Database.EnsureDeleted();
@ -76,17 +65,17 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
{ {
context = CreateTestContext(); context = CreateTestContext();
} }
return MockHelpers.CreateManager(new UserStore<TUser, TRole, ApplicationDbContext, TKey>((ApplicationDbContext)context)); return MockHelpers.CreateManager(new UserStore<TUser, TRole, TestDbContext, TKey>((TestDbContext)context));
} }
protected override RoleManager<TRole> CreateRoleManager(object context = null) protected override RoleManager<TRole> CreateRoleManager(object context = null)
{ {
var services = DbUtil.ConfigureDbServices<TestDbContext>(ConnectionString);
if (context == null) if (context == null)
{ {
context = CreateTestContext(); context = CreateTestContext();
} }
var services = UserStoreTest.ConfigureDbServices(ConnectionString); services.AddIdentity<TUser, TRole>().AddRoleStore(new RoleStore<TRole, TestDbContext, TKey>((TestDbContext)context));
services.AddIdentity<TUser, TRole>().AddRoleStore(new RoleStore<TRole, ApplicationDbContext, TKey>((ApplicationDbContext)context));
return services.BuildServiceProvider().GetService<RoleManager<TRole>>(); return services.BuildServiceProvider().GetService<RoleManager<TRole>>();
} }
@ -103,8 +92,8 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
builder.UseServices(services => builder.UseServices(services =>
{ {
UserStoreTest.ConfigureDbServices(ConnectionString, services); DbUtil.ConfigureDbServices<TestDbContext>(ConnectionString, services);
services.AddIdentitySqlServer<ApplicationDbContext, TUser, TRole, TKey>(); services.AddIdentityEntityFramework<TestDbContext, TUser, TRole, TKey>();
}); });
var userStore = builder.ApplicationServices.GetService<IUserStore<TUser>>(); var userStore = builder.ApplicationServices.GetService<IUserStore<TUser>>();
@ -128,8 +117,8 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
builder.UseServices(services => builder.UseServices(services =>
{ {
UserStoreTest.ConfigureDbServices(ConnectionString, services); DbUtil.ConfigureDbServices<TestDbContext>(ConnectionString, services);
services.AddIdentitySqlServer<ApplicationDbContext, TUser, TRole, TKey>(); services.AddIdentityEntityFramework<TestDbContext, TUser, TRole, TKey>();
services.ConfigureIdentity(options => services.ConfigureIdentity(options =>
{ {
options.Password.RequiredLength = 1; options.Password.RequiredLength = 1;

View File

@ -3,10 +3,8 @@
using System; using System;
using Microsoft.AspNet.Identity.Test; using Microsoft.AspNet.Identity.Test;
using Microsoft.Data.Entity.Services;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.DependencyInjection.Fallback; using Microsoft.Framework.DependencyInjection.Fallback;
using Microsoft.Framework.Logging;
using Xunit; using Xunit;
namespace Microsoft.AspNet.Identity.EntityFramework.Test namespace Microsoft.AspNet.Identity.EntityFramework.Test
@ -42,9 +40,9 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
} }
} }
public class ApplicationUserStore : UserStore<GuidUser, GuidRole, ApplicationDbContext, Guid> public class ApplicationUserStore : UserStore<GuidUser, GuidRole, TestDbContext, Guid>
{ {
public ApplicationUserStore(ApplicationDbContext context) : base(context) { } public ApplicationUserStore(TestDbContext context) : base(context) { }
public override Guid ConvertIdFromString(string userId) public override Guid ConvertIdFromString(string userId)
{ {
@ -52,9 +50,9 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
} }
} }
public class ApplicationRoleStore : RoleStore<GuidRole, ApplicationDbContext, Guid> public class ApplicationRoleStore : RoleStore<GuidRole, TestDbContext, Guid>
{ {
public ApplicationRoleStore(ApplicationDbContext context) : base(context) { } public ApplicationRoleStore(TestDbContext context) : base(context) { }
public override Guid ConvertIdFromString(string id) public override Guid ConvertIdFromString(string id)
{ {
@ -68,7 +66,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
{ {
context = CreateTestContext(); context = CreateTestContext();
} }
return MockHelpers.CreateManager(new ApplicationUserStore((ApplicationDbContext)context)); return MockHelpers.CreateManager(new ApplicationUserStore((TestDbContext)context));
} }
protected override RoleManager<GuidRole> CreateRoleManager(object context) protected override RoleManager<GuidRole> CreateRoleManager(object context)
@ -77,8 +75,8 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
{ {
context = CreateTestContext(); context = CreateTestContext();
} }
var services = UserStoreTest.ConfigureDbServices(ConnectionString); var services = DbUtil.ConfigureDbServices(ConnectionString);
services.AddIdentity<GuidUser, GuidRole>().AddRoleStore(new ApplicationRoleStore((ApplicationDbContext)context)); services.AddIdentity<GuidUser, GuidRole>().AddRoleStore(new ApplicationRoleStore((TestDbContext)context));
return services.BuildServiceProvider().GetService<RoleManager<GuidRole>>(); return services.BuildServiceProvider().GetService<RoleManager<GuidRole>>();
} }
} }

View File

@ -1,11 +1,6 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // 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. // 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 System;
using Xunit; using Xunit;

View File

@ -6,29 +6,24 @@ using System.Linq;
using System.Security.Claims; using System.Security.Claims;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Identity.Test; using Microsoft.AspNet.Identity.Test;
using Microsoft.Data.Entity; using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Services; using Microsoft.Data.Entity.Services;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.DependencyInjection.Fallback; using Microsoft.Framework.DependencyInjection.Fallback;
using Microsoft.Framework.Logging; using Microsoft.Framework.Logging;
using Microsoft.Framework.OptionsModel;
using Xunit; using Xunit;
namespace Microsoft.AspNet.Identity.EntityFramework.Test namespace Microsoft.AspNet.Identity.EntityFramework.Test
{ {
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { }
[TestCaseOrderer("Microsoft.AspNet.Identity.Test.PriorityOrderer", "Microsoft.AspNet.Identity.EntityFramework.Test")] [TestCaseOrderer("Microsoft.AspNet.Identity.Test.PriorityOrderer", "Microsoft.AspNet.Identity.EntityFramework.Test")]
public class UserStoreTest : UserManagerTestBase<IdentityUser, IdentityRole> public class UserStoreTest : UserManagerTestBase<IdentityUser, IdentityRole>
{ {
private readonly string ConnectionString = @"Server=(localdb)\v11.0;Database=SqlUserStoreTest" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year + ";Trusted_Connection=True;"; 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<ApplicationUser>
{
public ApplicationDbContext(IServiceProvider services, IOptions<DbContextOptions> options) : base(services, options.Options) { }
}
[TestPriority(-1000)] [TestPriority(-1000)]
[Fact] [Fact]
public void DropDatabaseStart() public void DropDatabaseStart()
@ -45,9 +40,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
public void DropDb() public void DropDb()
{ {
var serviceProvider = ConfigureDbServices(ConnectionString).BuildServiceProvider(); var db = DbUtil.Create<ApplicationDbContext>(ConnectionString);
var db = new ApplicationDbContext(serviceProvider,
serviceProvider.GetService<IOptions<DbContextOptions>>());
db.Database.EnsureDeleted(); db.Database.EnsureDeleted();
} }
@ -59,8 +52,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
builder.UseServices(services => builder.UseServices(services =>
{ {
ConfigureDbServices(ConnectionString, services); DbUtil.ConfigureDbServices<ApplicationDbContext>(ConnectionString, services);
services.AddScoped<ApplicationDbContext>();
services.AddDefaultIdentity<ApplicationDbContext, ApplicationUser, IdentityRole>(); services.AddDefaultIdentity<ApplicationDbContext, ApplicationUser, IdentityRole>();
}); });
@ -86,8 +78,11 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
builder.UseServices(services => builder.UseServices(services =>
{ {
services.AddInstance<ILoggerFactory>(new NullLoggerFactory()); services.AddInstance<ILoggerFactory>(new NullLoggerFactory());
services.AddEntityFramework().AddSqlServer(); services.Add(HostingServices.GetDefaultServices());
services.AddIdentitySqlServer<ApplicationDbContext, ApplicationUser>(options => services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(ConnectionString));
services.AddIdentityEntityFramework<ApplicationDbContext, ApplicationUser>(options =>
{ {
options.Password.RequiredLength = 1; options.Password.RequiredLength = 1;
options.Password.RequireLowercase = false; options.Password.RequireLowercase = false;
@ -95,8 +90,6 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
options.Password.RequireUppercase = false; options.Password.RequireUppercase = false;
options.Password.RequireDigit = false; options.Password.RequireDigit = false;
}); });
services.Configure<DbContextOptions>(options =>
options.UseSqlServer(ConnectionString));
}); });
var userStore = builder.ApplicationServices.GetService<IUserStore<ApplicationUser>>(); var userStore = builder.ApplicationServices.GetService<IUserStore<ApplicationUser>>();
@ -127,8 +120,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
public IdentityDbContext CreateContext(bool delete = false) public IdentityDbContext CreateContext(bool delete = false)
{ {
var serviceProvider = ConfigureDbServices(ConnectionString).BuildServiceProvider(); var db = DbUtil.Create<IdentityDbContext>(ConnectionString);
var db = new IdentityDbContext(serviceProvider, serviceProvider.GetService<IOptions<DbContextOptions>>().Options);
if (delete) if (delete)
{ {
db.Database.EnsureDeleted(); db.Database.EnsureDeleted();
@ -147,25 +139,9 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
CreateContext(); 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<ILoggerFactory>(new NullLoggerFactory());
services.Configure<DbContextOptions>(options =>
options.UseSqlServer(connectionString));
return services;
}
public ApplicationDbContext CreateAppContext() public ApplicationDbContext CreateAppContext()
{ {
CreateContext(); var db = DbUtil.Create<ApplicationDbContext>(ConnectionString);
var serviceProvider = ConfigureDbServices(ConnectionString).BuildServiceProvider();
var db = new ApplicationDbContext(serviceProvider, serviceProvider.GetService<IOptions<DbContextOptions>>());
db.Database.EnsureCreated(); db.Database.EnsureCreated();
return db; return db;
} }
@ -186,7 +162,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
public RoleManager<IdentityRole> CreateRoleManager(IdentityDbContext context) public RoleManager<IdentityRole> CreateRoleManager(IdentityDbContext context)
{ {
var services = ConfigureDbServices(ConnectionString); var services = DbUtil.ConfigureDbServices(ConnectionString);
services.AddIdentity().AddRoleStore(new RoleStore<IdentityRole>(context)); services.AddIdentity().AddRoleStore(new RoleStore<IdentityRole>(context));
return services.BuildServiceProvider().GetService<RoleManager<IdentityRole>>(); return services.BuildServiceProvider().GetService<RoleManager<IdentityRole>>();
} }

View File

@ -1,5 +1,6 @@
{ {
"dependencies": { "dependencies": {
"Microsoft.AspNet.Hosting": "1.0.0-*",
"Microsoft.AspNet.Http": "1.0.0-*", "Microsoft.AspNet.Http": "1.0.0-*",
"Microsoft.AspNet.Identity": "3.0.0-*", "Microsoft.AspNet.Identity": "3.0.0-*",
"Microsoft.AspNet.Identity.EntityFramework": "1.0.0-*", "Microsoft.AspNet.Identity.EntityFramework": "1.0.0-*",

View File

@ -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 { }
}