From 980206cc260a438b390ca5154fee6ea236b171bc Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Wed, 21 Jun 2017 16:47:44 -0700 Subject: [PATCH] Revert IdentityDbContext breaking change (#1286) --- .../IdentityDbContext.cs | 118 +-------------- ...dentityEntityFrameworkBuilderExtensions.cs | 2 +- .../IdentityUserContext.cs | 140 ++++++++++++++++++ .../InMemoryContext.cs | 2 +- .../SqlStoreOnlyUsersTestBase.cs | 2 +- 5 files changed, 145 insertions(+), 119 deletions(-) create mode 100644 src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/IdentityUserContext.cs diff --git a/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/IdentityDbContext.cs b/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/IdentityDbContext.cs index 8044d33d09..6bd4325a34 100644 --- a/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/IdentityDbContext.cs +++ b/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/IdentityDbContext.cs @@ -27,7 +27,7 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore /// Base class for the Entity Framework database context used for identity. /// /// The type of the user objects. - public class IdentityDbContext : IdentityDbContext where TUser : IdentityUser + public class IdentityDbContext : IdentityDbContext where TUser : IdentityUser { /// /// Initializes a new instance of . @@ -41,27 +41,6 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore protected IdentityDbContext() { } } - /// - /// Base class for the Entity Framework database context used for identity. - /// - /// The type of user objects. - /// The type of the primary key for users and roles. - public class IdentityDbContext : IdentityDbContext, IdentityUserLogin, IdentityUserToken> - where TUser : IdentityUser - where TKey : IEquatable - { - /// - /// Initializes a new instance of the db context. - /// - /// The options to be used by a . - public IdentityDbContext(DbContextOptions options) : base(options) { } - - /// - /// Initializes a new instance of the class. - /// - protected IdentityDbContext() { } - } - /// /// Base class for the Entity Framework database context used for identity. /// @@ -85,99 +64,6 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore protected IdentityDbContext() { } } - /// - /// Base class for the Entity Framework database context used for identity. - /// - /// The type of user objects. - /// The type of the primary key for users and roles. - /// The type of the user claim object. - /// The type of the user login object. - /// The type of the user token object. - public abstract class IdentityDbContext : DbContext - where TUser : IdentityUser - where TKey : IEquatable - where TUserClaim : IdentityUserClaim - where TUserLogin : IdentityUserLogin - where TUserToken : IdentityUserToken - { - /// - /// Initializes a new instance of the class. - /// - /// The options to be used by a . - public IdentityDbContext(DbContextOptions options) : base(options) { } - - /// - /// Initializes a new instance of the class. - /// - protected IdentityDbContext() { } - - /// - /// Gets or sets the of Users. - /// - public DbSet Users { get; set; } - - /// - /// Gets or sets the of User claims. - /// - public DbSet UserClaims { get; set; } - - /// - /// Gets or sets the of User logins. - /// - public DbSet UserLogins { get; set; } - - /// - /// Gets or sets the of User tokens. - /// - public DbSet UserTokens { get; set; } - - /// - /// Configures the schema needed for the identity framework. - /// - /// - /// The builder being used to construct the model for this context. - /// - protected override void OnModelCreating(ModelBuilder builder) - { - builder.Entity(b => - { - b.HasKey(u => u.Id); - b.HasIndex(u => u.NormalizedUserName).HasName("UserNameIndex").IsUnique(); - b.HasIndex(u => u.NormalizedEmail).HasName("EmailIndex"); - b.ToTable("AspNetUsers"); - b.Property(u => u.ConcurrencyStamp).IsConcurrencyToken(); - - b.Property(u => u.UserName).HasMaxLength(256); - b.Property(u => u.NormalizedUserName).HasMaxLength(256); - b.Property(u => u.Email).HasMaxLength(256); - b.Property(u => u.NormalizedEmail).HasMaxLength(256); - - // Replace with b.HasMany(). - b.HasMany().WithOne().HasForeignKey(uc => uc.UserId).IsRequired(); - b.HasMany().WithOne().HasForeignKey(ul => ul.UserId).IsRequired(); - b.HasMany().WithOne().HasForeignKey(ut => ut.UserId).IsRequired(); - }); - - builder.Entity(b => - { - b.HasKey(uc => uc.Id); - b.ToTable("AspNetUserClaims"); - }); - - builder.Entity(b => - { - b.HasKey(l => new { l.LoginProvider, l.ProviderKey }); - b.ToTable("AspNetUserLogins"); - }); - - builder.Entity(b => - { - b.HasKey(l => new { l.UserId, l.LoginProvider, l.Name }); - b.ToTable("AspNetUserTokens"); - }); - } - } - /// /// Base class for the Entity Framework database context used for identity. /// @@ -189,7 +75,7 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore /// The type of the user login object. /// The type of the role claim object. /// The type of the user token object. - public abstract class IdentityDbContext : IdentityDbContext + public abstract class IdentityDbContext : IdentityUserContext where TUser : IdentityUser where TRole : IdentityRole where TKey : IEquatable diff --git a/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/IdentityEntityFrameworkBuilderExtensions.cs b/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/IdentityEntityFrameworkBuilderExtensions.cs index 0c7d1d48fc..23933d9d57 100644 --- a/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/IdentityEntityFrameworkBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/IdentityEntityFrameworkBuilderExtensions.cs @@ -75,7 +75,7 @@ namespace Microsoft.Extensions.DependencyInjection else { // No Roles Type userStoreType = null; - var identityContext = FindGenericBaseType(contextType, typeof(IdentityDbContext<,,,,>)); + var identityContext = FindGenericBaseType(contextType, typeof(IdentityUserContext<,,,,>)); if (identityContext == null) { // If its a custom DbContext, we can only add the default POCOs diff --git a/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/IdentityUserContext.cs b/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/IdentityUserContext.cs new file mode 100644 index 0000000000..2142b52fb1 --- /dev/null +++ b/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/IdentityUserContext.cs @@ -0,0 +1,140 @@ +// 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 Microsoft.EntityFrameworkCore; + +namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore +{ + /// + /// Base class for the Entity Framework database context used for identity. + /// + /// The type of the user objects. + public class IdentityUserContext : IdentityUserContext where TUser : IdentityUser + { + /// + /// Initializes a new instance of . + /// + /// The options to be used by a . + public IdentityUserContext(DbContextOptions options) : base(options) { } + + /// + /// Initializes a new instance of the class. + /// + protected IdentityUserContext() { } + } + + /// + /// Base class for the Entity Framework database context used for identity. + /// + /// The type of user objects. + /// The type of the primary key for users and roles. + public class IdentityUserContext : IdentityUserContext, IdentityUserLogin, IdentityUserToken> + where TUser : IdentityUser + where TKey : IEquatable + { + /// + /// Initializes a new instance of the db context. + /// + /// The options to be used by a . + public IdentityUserContext(DbContextOptions options) : base(options) { } + + /// + /// Initializes a new instance of the class. + /// + protected IdentityUserContext() { } + } + + /// + /// Base class for the Entity Framework database context used for identity. + /// + /// The type of user objects. + /// The type of the primary key for users and roles. + /// The type of the user claim object. + /// The type of the user login object. + /// The type of the user token object. + public abstract class IdentityUserContext : DbContext + where TUser : IdentityUser + where TKey : IEquatable + where TUserClaim : IdentityUserClaim + where TUserLogin : IdentityUserLogin + where TUserToken : IdentityUserToken + { + /// + /// Initializes a new instance of the class. + /// + /// The options to be used by a . + public IdentityUserContext(DbContextOptions options) : base(options) { } + + /// + /// Initializes a new instance of the class. + /// + protected IdentityUserContext() { } + + /// + /// Gets or sets the of Users. + /// + public DbSet Users { get; set; } + + /// + /// Gets or sets the of User claims. + /// + public DbSet UserClaims { get; set; } + + /// + /// Gets or sets the of User logins. + /// + public DbSet UserLogins { get; set; } + + /// + /// Gets or sets the of User tokens. + /// + public DbSet UserTokens { get; set; } + + /// + /// Configures the schema needed for the identity framework. + /// + /// + /// The builder being used to construct the model for this context. + /// + protected override void OnModelCreating(ModelBuilder builder) + { + builder.Entity(b => + { + b.HasKey(u => u.Id); + b.HasIndex(u => u.NormalizedUserName).HasName("UserNameIndex").IsUnique(); + b.HasIndex(u => u.NormalizedEmail).HasName("EmailIndex"); + b.ToTable("AspNetUsers"); + b.Property(u => u.ConcurrencyStamp).IsConcurrencyToken(); + + b.Property(u => u.UserName).HasMaxLength(256); + b.Property(u => u.NormalizedUserName).HasMaxLength(256); + b.Property(u => u.Email).HasMaxLength(256); + b.Property(u => u.NormalizedEmail).HasMaxLength(256); + + // Replace with b.HasMany(). + b.HasMany().WithOne().HasForeignKey(uc => uc.UserId).IsRequired(); + b.HasMany().WithOne().HasForeignKey(ul => ul.UserId).IsRequired(); + b.HasMany().WithOne().HasForeignKey(ut => ut.UserId).IsRequired(); + }); + + builder.Entity(b => + { + b.HasKey(uc => uc.Id); + b.ToTable("AspNetUserClaims"); + }); + + builder.Entity(b => + { + b.HasKey(l => new { l.LoginProvider, l.ProviderKey }); + b.ToTable("AspNetUserLogins"); + }); + + builder.Entity(b => + { + b.HasKey(l => new { l.UserId, l.LoginProvider, l.Name }); + b.ToTable("AspNetUserTokens"); + }); + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test/InMemoryContext.cs b/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test/InMemoryContext.cs index d1191cfbbf..3f8cfe61d3 100644 --- a/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test/InMemoryContext.cs +++ b/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test/InMemoryContext.cs @@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test } public class InMemoryContext : - IdentityDbContext + IdentityUserContext where TUser : IdentityUser { public InMemoryContext(DbContextOptions options) : base(options) diff --git a/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test/SqlStoreOnlyUsersTestBase.cs b/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test/SqlStoreOnlyUsersTestBase.cs index 4df7679aa9..9573721fb6 100644 --- a/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test/SqlStoreOnlyUsersTestBase.cs +++ b/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test/SqlStoreOnlyUsersTestBase.cs @@ -32,7 +32,7 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test return TestPlatformHelper.IsMono || !TestPlatformHelper.IsWindows; } - public class TestUserDbContext : IdentityDbContext + public class TestUserDbContext : IdentityUserContext { public TestUserDbContext(DbContextOptions options) : base(options) { } }