diff --git a/src/Microsoft.AspNet.Identity.SqlServer/IdentityDbContext.cs b/src/Microsoft.AspNet.Identity.SqlServer/IdentityDbContext.cs index a940ddd726..3ec10bd4a5 100644 --- a/src/Microsoft.AspNet.Identity.SqlServer/IdentityDbContext.cs +++ b/src/Microsoft.AspNet.Identity.SqlServer/IdentityDbContext.cs @@ -12,7 +12,6 @@ namespace Microsoft.AspNet.Identity.SqlServer { public IdentityDbContext() { } public IdentityDbContext(IServiceProvider serviceProvider) : base(serviceProvider) { } - public IdentityDbContext(IServiceProvider serviceProvider, string nameOrConnectionString) : base(serviceProvider, nameOrConnectionString) { } public IdentityDbContext(DbContextOptions options) : base(options) { } public IdentityDbContext(IServiceProvider serviceProvider, DbContextOptions options) : base(serviceProvider, options) { } } @@ -23,7 +22,6 @@ namespace Microsoft.AspNet.Identity.SqlServer { public IdentityDbContext() { } public IdentityDbContext(IServiceProvider serviceProvider) : base(serviceProvider) { } - public IdentityDbContext(IServiceProvider serviceProvider, string nameOrConnectionString) : base(serviceProvider, nameOrConnectionString) { } public IdentityDbContext(DbContextOptions options) : base(options) { } public IdentityDbContext(IServiceProvider serviceProvider, DbContextOptions options) : base(serviceProvider, options) { } } @@ -40,25 +38,11 @@ namespace Microsoft.AspNet.Identity.SqlServer public DbSet Roles { get; set; } public DbSet> RoleClaims { get; set; } - private readonly string _nameOrConnectionString; - public IdentityDbContext() { } - public IdentityDbContext(IServiceProvider serviceProvider, string nameOrConnectionString) : base(serviceProvider) - { - _nameOrConnectionString = nameOrConnectionString; - } public IdentityDbContext(IServiceProvider serviceProvider) : base(serviceProvider) { } public IdentityDbContext(DbContextOptions options) : base(options) { } public IdentityDbContext(IServiceProvider serviceProvider, DbContextOptions options) : base(serviceProvider, options) { } - protected override void OnConfiguring(DbContextOptions builder) - { - if (!string.IsNullOrEmpty(_nameOrConnectionString)) - { - builder.UseSqlServer(_nameOrConnectionString); - } - } - protected override void OnModelCreating(ModelBuilder builder) { builder.Entity(b => diff --git a/test/Microsoft.AspNet.Identity.SqlServer.Test/CustomPocoTest.cs b/test/Microsoft.AspNet.Identity.SqlServer.Test/CustomPocoTest.cs index 00cdc6ce74..6a3bff428d 100644 --- a/test/Microsoft.AspNet.Identity.SqlServer.Test/CustomPocoTest.cs +++ b/test/Microsoft.AspNet.Identity.SqlServer.Test/CustomPocoTest.cs @@ -1,10 +1,11 @@ // 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.Data.Entity.Metadata; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection.Fallback; +using Microsoft.Framework.OptionsModel; using System; using System.Linq; using System.Threading.Tasks; @@ -12,9 +13,10 @@ using Xunit; namespace Microsoft.AspNet.Identity.SqlServer.Test { + [TestCaseOrderer("Microsoft.AspNet.Identity.Test.PriorityOrderer", "Microsoft.AspNet.Identity.SqlServer.Test")] public class CustomPocoTest { - private const string ConnectionString = @"Server=(localdb)\v11.0;Database=CustomPocoTest;Trusted_Connection=True;"; + private readonly string ConnectionString = @"Server=(localdb)\v11.0;Database=CustomUserContextTest" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year + ";Trusted_Connection=True;"; public class User where TKey : IEquatable { @@ -23,33 +25,27 @@ namespace Microsoft.AspNet.Identity.SqlServer.Test } public class CustomDbContext : DbContext where TUser : class - //where TUser : User where TKey : IEquatable { public DbSet Users { get; set; } - public CustomDbContext(IServiceProvider services) : base(services) { } - - protected override void OnConfiguring(DbContextOptions builder) - { - builder.UseSqlServer(ConnectionString); - } - - //protected override void OnModelCreating(ModelBuilder builder) - //{ - // builder.Entity() - // .Key(u => u.Id) - // .Properties(ps => ps.Property(u => u.UserName)); - //} + public CustomDbContext(IServiceProvider services) : + base(services, services.GetService>().Options) { } } - //public static CustomDbContext, TKey> CreateContext(bool delete = false) where TKey : IEquatable - public static CustomDbContext CreateContext(bool delete = false) where TUser : class + + public CustomDbContext GetContext() where TUser : class { var services = new ServiceCollection(); + services.Add(OptionsServices.GetDefaultServices()); services.AddEntityFramework().AddSqlServer(); + services.SetupOptions(options => options.UseSqlServer(ConnectionString)); var serviceProvider = services.BuildServiceProvider(); + return new CustomDbContext(serviceProvider); + } - var db = new CustomDbContext(serviceProvider); + public CustomDbContext CreateContext(bool delete = false) where TUser : class + { + var db = GetContext(); if (delete) { db.Database.EnsureDeleted(); @@ -58,6 +54,26 @@ namespace Microsoft.AspNet.Identity.SqlServer.Test return db; } + [TestPriority(-1000)] + [Fact] + public void DropDatabaseStart() + { + DropDb(); + } + + [TestPriority(10000)] + [Fact] + public void DropDatabaseDone() + { + DropDb(); + } + + public void DropDb() + { + var db = GetContext>(); + db.Database.EnsureDeleted(); + } + [Fact] public async Task CanUpdateNameGuid() { diff --git a/test/Microsoft.AspNet.Identity.SqlServer.Test/DefaultPocoTest.cs b/test/Microsoft.AspNet.Identity.SqlServer.Test/DefaultPocoTest.cs index b4f1ddb7ee..2269843c2e 100644 --- a/test/Microsoft.AspNet.Identity.SqlServer.Test/DefaultPocoTest.cs +++ b/test/Microsoft.AspNet.Identity.SqlServer.Test/DefaultPocoTest.cs @@ -6,32 +6,44 @@ 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.Threading.Tasks; using Xunit; namespace Microsoft.AspNet.Identity.SqlServer.Test { + [TestCaseOrderer("Microsoft.AspNet.Identity.Test.PriorityOrderer", "Microsoft.AspNet.Identity.SqlServer.Test")] public class DefaultPocoTest { - private const string ConnectionString = @"Server=(localdb)\v11.0;Database=DefaultPocoTest;Trusted_Connection=True;"; - public static IdentityDbContext CreateContext(bool delete = false) + 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 = new ServiceCollection(); + services.Add(OptionsServices.GetDefaultServices()); services.AddEntityFramework().AddSqlServer(); + services.SetupOptions(options => options.UseSqlServer(ConnectionString)); var serviceProvider = services.BuildServiceProvider(); - - var db = new IdentityDbContext(serviceProvider, ConnectionString); - if (delete) + var db = new IdentityDbContext(serviceProvider, + serviceProvider.GetService>().Options); + if (ensureCreated) { - db.Database.EnsureDeleted(); + db.Database.EnsureCreated(); } - db.Database.EnsureCreated(); return db; } - public static void EnsureDatabase() + public void DropDb() { - CreateContext(); + var db = CreateContext(); + db.Database.EnsureDeleted(); + } + + [TestPriority(-1000)] + [Fact] + public void DropDatabaseStart() + { + DropDb(); } [Fact] @@ -62,5 +74,12 @@ namespace Microsoft.AspNet.Identity.SqlServer.Test IdentityResultAssert.IsSuccess(await userManager.CreateAsync(user, password)); IdentityResultAssert.IsSuccess(await userManager.DeleteAsync(user)); } + + [TestPriority(10000)] + [Fact] + public void DropDatabaseDone() + { + DropDb(); + } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Identity.SqlServer.Test/UserStoreGuidKeyTest.cs b/test/Microsoft.AspNet.Identity.SqlServer.Test/UserStoreGuidKeyTest.cs index 0e2ff36f06..38fbaebb60 100644 --- a/test/Microsoft.AspNet.Identity.SqlServer.Test/UserStoreGuidKeyTest.cs +++ b/test/Microsoft.AspNet.Identity.SqlServer.Test/UserStoreGuidKeyTest.cs @@ -30,11 +30,13 @@ namespace Microsoft.AspNet.Identity.SqlServer.Test [TestCaseOrderer("Microsoft.AspNet.Identity.Test.PriorityOrderer", "Microsoft.AspNet.Identity.SqlServer.Test")] public class UserStoreGuidTest : SqlStoreTestBase { + private readonly string _connectionString = @"Server=(localdb)\v11.0;Database=SqlUserStoreGuidTest" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year + ";Trusted_Connection=True;"; + public override string ConnectionString { get { - return @"Server=(localdb)\v11.0;Database=SqlUserStoreGuidTest;Trusted_Connection=True;"; + return _connectionString; } } diff --git a/test/Microsoft.AspNet.Identity.SqlServer.Test/UserStoreIntKeyTest.cs b/test/Microsoft.AspNet.Identity.SqlServer.Test/UserStoreIntKeyTest.cs index ae1272e02d..703ae41341 100644 --- a/test/Microsoft.AspNet.Identity.SqlServer.Test/UserStoreIntKeyTest.cs +++ b/test/Microsoft.AspNet.Identity.SqlServer.Test/UserStoreIntKeyTest.cs @@ -30,11 +30,13 @@ namespace Microsoft.AspNet.Identity.SqlServer.Test [TestCaseOrderer("Microsoft.AspNet.Identity.Test.PriorityOrderer", "Microsoft.AspNet.Identity.SqlServer.Test")] public class UserStoreIntTest : SqlStoreTestBase { + private readonly string _connectionString = @"Server=(localdb)\v11.0;Database=SqlUserStoreIntTest" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year + ";Trusted_Connection=True;"; + public override string ConnectionString { get { - return @"Server=(localdb)\v11.0;Database=SqlUserStoreIntTest;Trusted_Connection=True;"; + return _connectionString; } } } diff --git a/test/Microsoft.AspNet.Identity.SqlServer.Test/UserStoreStringKeyTest.cs b/test/Microsoft.AspNet.Identity.SqlServer.Test/UserStoreStringKeyTest.cs index 5ff81a367b..aa2ce411f6 100644 --- a/test/Microsoft.AspNet.Identity.SqlServer.Test/UserStoreStringKeyTest.cs +++ b/test/Microsoft.AspNet.Identity.SqlServer.Test/UserStoreStringKeyTest.cs @@ -28,11 +28,13 @@ namespace Microsoft.AspNet.Identity.SqlServer.Test [TestCaseOrderer("Microsoft.AspNet.Identity.Test.PriorityOrderer", "Microsoft.AspNet.Identity.SqlServer.Test")] public class UserStoreStringKeyTest : SqlStoreTestBase { + private readonly string _connectionString = @"Server=(localdb)\v11.0;Database=SqlUserStoreStringTest" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year + ";Trusted_Connection=True;"; + public override string ConnectionString { get { - return @"Server=(localdb)\v11.0;Database=SqlUserStoreStringTest;Trusted_Connection=True;"; + return _connectionString; } } } diff --git a/test/Microsoft.AspNet.Identity.SqlServer.Test/UserStoreTest.cs b/test/Microsoft.AspNet.Identity.SqlServer.Test/UserStoreTest.cs index f7c53e60b7..1be62f5c14 100644 --- a/test/Microsoft.AspNet.Identity.SqlServer.Test/UserStoreTest.cs +++ b/test/Microsoft.AspNet.Identity.SqlServer.Test/UserStoreTest.cs @@ -21,7 +21,7 @@ namespace Microsoft.AspNet.Identity.SqlServer.Test [TestCaseOrderer("Microsoft.AspNet.Identity.Test.PriorityOrderer", "Microsoft.AspNet.Identity.SqlServer.Test")] public class UserStoreTest : UserManagerTestBase { - private static readonly string ConnectionString = @"Server=(localdb)\v11.0;Database=SqlUserStoreTest;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 { } @@ -135,9 +135,10 @@ namespace Microsoft.AspNet.Identity.SqlServer.Test { var services = new ServiceCollection(); services.AddEntityFramework().AddSqlServer(); + var dbOptions = new DbContextOptions(); + dbOptions.UseSqlServer(ConnectionString); var serviceProvider = services.BuildServiceProvider(); - - var db = new IdentityDbContext(serviceProvider, ConnectionString); + var db = new IdentityDbContext(serviceProvider, dbOptions); if (delete) { db.Database.EnsureDeleted();