Skip to content

Commit 980206c

Browse files
authored
Revert IdentityDbContext breaking change (#1286)
1 parent f555a26 commit 980206c

File tree

5 files changed

+145
-119
lines changed

5 files changed

+145
-119
lines changed

src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/IdentityDbContext.cs

Lines changed: 2 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected IdentityDbContext() { }
2727
/// Base class for the Entity Framework database context used for identity.
2828
/// </summary>
2929
/// <typeparam name="TUser">The type of the user objects.</typeparam>
30-
public class IdentityDbContext<TUser> : IdentityDbContext<TUser, string> where TUser : IdentityUser
30+
public class IdentityDbContext<TUser> : IdentityDbContext<TUser, IdentityRole, string> where TUser : IdentityUser
3131
{
3232
/// <summary>
3333
/// Initializes a new instance of <see cref="IdentityDbContext"/>.
@@ -41,27 +41,6 @@ public IdentityDbContext(DbContextOptions options) : base(options) { }
4141
protected IdentityDbContext() { }
4242
}
4343

44-
/// <summary>
45-
/// Base class for the Entity Framework database context used for identity.
46-
/// </summary>
47-
/// <typeparam name="TUser">The type of user objects.</typeparam>
48-
/// <typeparam name="TKey">The type of the primary key for users and roles.</typeparam>
49-
public class IdentityDbContext<TUser, TKey> : IdentityDbContext<TUser, TKey, IdentityUserClaim<TKey>, IdentityUserLogin<TKey>, IdentityUserToken<TKey>>
50-
where TUser : IdentityUser<TKey>
51-
where TKey : IEquatable<TKey>
52-
{
53-
/// <summary>
54-
/// Initializes a new instance of the db context.
55-
/// </summary>
56-
/// <param name="options">The options to be used by a <see cref="DbContext"/>.</param>
57-
public IdentityDbContext(DbContextOptions options) : base(options) { }
58-
59-
/// <summary>
60-
/// Initializes a new instance of the class.
61-
/// </summary>
62-
protected IdentityDbContext() { }
63-
}
64-
6544
/// <summary>
6645
/// Base class for the Entity Framework database context used for identity.
6746
/// </summary>
@@ -85,99 +64,6 @@ public IdentityDbContext(DbContextOptions options) : base(options) { }
8564
protected IdentityDbContext() { }
8665
}
8766

88-
/// <summary>
89-
/// Base class for the Entity Framework database context used for identity.
90-
/// </summary>
91-
/// <typeparam name="TUser">The type of user objects.</typeparam>
92-
/// <typeparam name="TKey">The type of the primary key for users and roles.</typeparam>
93-
/// <typeparam name="TUserClaim">The type of the user claim object.</typeparam>
94-
/// <typeparam name="TUserLogin">The type of the user login object.</typeparam>
95-
/// <typeparam name="TUserToken">The type of the user token object.</typeparam>
96-
public abstract class IdentityDbContext<TUser, TKey, TUserClaim, TUserLogin, TUserToken> : DbContext
97-
where TUser : IdentityUser<TKey>
98-
where TKey : IEquatable<TKey>
99-
where TUserClaim : IdentityUserClaim<TKey>
100-
where TUserLogin : IdentityUserLogin<TKey>
101-
where TUserToken : IdentityUserToken<TKey>
102-
{
103-
/// <summary>
104-
/// Initializes a new instance of the class.
105-
/// </summary>
106-
/// <param name="options">The options to be used by a <see cref="DbContext"/>.</param>
107-
public IdentityDbContext(DbContextOptions options) : base(options) { }
108-
109-
/// <summary>
110-
/// Initializes a new instance of the class.
111-
/// </summary>
112-
protected IdentityDbContext() { }
113-
114-
/// <summary>
115-
/// Gets or sets the <see cref="DbSet{TEntity}"/> of Users.
116-
/// </summary>
117-
public DbSet<TUser> Users { get; set; }
118-
119-
/// <summary>
120-
/// Gets or sets the <see cref="DbSet{TEntity}"/> of User claims.
121-
/// </summary>
122-
public DbSet<TUserClaim> UserClaims { get; set; }
123-
124-
/// <summary>
125-
/// Gets or sets the <see cref="DbSet{TEntity}"/> of User logins.
126-
/// </summary>
127-
public DbSet<TUserLogin> UserLogins { get; set; }
128-
129-
/// <summary>
130-
/// Gets or sets the <see cref="DbSet{TEntity}"/> of User tokens.
131-
/// </summary>
132-
public DbSet<TUserToken> UserTokens { get; set; }
133-
134-
/// <summary>
135-
/// Configures the schema needed for the identity framework.
136-
/// </summary>
137-
/// <param name="builder">
138-
/// The builder being used to construct the model for this context.
139-
/// </param>
140-
protected override void OnModelCreating(ModelBuilder builder)
141-
{
142-
builder.Entity<TUser>(b =>
143-
{
144-
b.HasKey(u => u.Id);
145-
b.HasIndex(u => u.NormalizedUserName).HasName("UserNameIndex").IsUnique();
146-
b.HasIndex(u => u.NormalizedEmail).HasName("EmailIndex");
147-
b.ToTable("AspNetUsers");
148-
b.Property(u => u.ConcurrencyStamp).IsConcurrencyToken();
149-
150-
b.Property(u => u.UserName).HasMaxLength(256);
151-
b.Property(u => u.NormalizedUserName).HasMaxLength(256);
152-
b.Property(u => u.Email).HasMaxLength(256);
153-
b.Property(u => u.NormalizedEmail).HasMaxLength(256);
154-
155-
// Replace with b.HasMany<IdentityUserClaim>().
156-
b.HasMany<TUserClaim>().WithOne().HasForeignKey(uc => uc.UserId).IsRequired();
157-
b.HasMany<TUserLogin>().WithOne().HasForeignKey(ul => ul.UserId).IsRequired();
158-
b.HasMany<TUserToken>().WithOne().HasForeignKey(ut => ut.UserId).IsRequired();
159-
});
160-
161-
builder.Entity<TUserClaim>(b =>
162-
{
163-
b.HasKey(uc => uc.Id);
164-
b.ToTable("AspNetUserClaims");
165-
});
166-
167-
builder.Entity<TUserLogin>(b =>
168-
{
169-
b.HasKey(l => new { l.LoginProvider, l.ProviderKey });
170-
b.ToTable("AspNetUserLogins");
171-
});
172-
173-
builder.Entity<TUserToken>(b =>
174-
{
175-
b.HasKey(l => new { l.UserId, l.LoginProvider, l.Name });
176-
b.ToTable("AspNetUserTokens");
177-
});
178-
}
179-
}
180-
18167
/// <summary>
18268
/// Base class for the Entity Framework database context used for identity.
18369
/// </summary>
@@ -189,7 +75,7 @@ protected override void OnModelCreating(ModelBuilder builder)
18975
/// <typeparam name="TUserLogin">The type of the user login object.</typeparam>
19076
/// <typeparam name="TRoleClaim">The type of the role claim object.</typeparam>
19177
/// <typeparam name="TUserToken">The type of the user token object.</typeparam>
192-
public abstract class IdentityDbContext<TUser, TRole, TKey, TUserClaim, TUserRole, TUserLogin, TRoleClaim, TUserToken> : IdentityDbContext<TUser, TKey, TUserClaim, TUserLogin, TUserToken>
78+
public abstract class IdentityDbContext<TUser, TRole, TKey, TUserClaim, TUserRole, TUserLogin, TRoleClaim, TUserToken> : IdentityUserContext<TUser, TKey, TUserClaim, TUserLogin, TUserToken>
19379
where TUser : IdentityUser<TKey>
19480
where TRole : IdentityRole<TKey>
19581
where TKey : IEquatable<TKey>

src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/IdentityEntityFrameworkBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private static void AddStores(IServiceCollection services, Type userType, Type r
7575
else
7676
{ // No Roles
7777
Type userStoreType = null;
78-
var identityContext = FindGenericBaseType(contextType, typeof(IdentityDbContext<,,,,>));
78+
var identityContext = FindGenericBaseType(contextType, typeof(IdentityUserContext<,,,,>));
7979
if (identityContext == null)
8080
{
8181
// If its a custom DbContext, we can only add the default POCOs
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.EntityFrameworkCore;
6+
7+
namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore
8+
{
9+
/// <summary>
10+
/// Base class for the Entity Framework database context used for identity.
11+
/// </summary>
12+
/// <typeparam name="TUser">The type of the user objects.</typeparam>
13+
public class IdentityUserContext<TUser> : IdentityUserContext<TUser, string> where TUser : IdentityUser
14+
{
15+
/// <summary>
16+
/// Initializes a new instance of <see cref="IdentityUserContext{TUser}"/>.
17+
/// </summary>
18+
/// <param name="options">The options to be used by a <see cref="DbContext"/>.</param>
19+
public IdentityUserContext(DbContextOptions options) : base(options) { }
20+
21+
/// <summary>
22+
/// Initializes a new instance of the <see cref="IdentityUserContext{TUser}" /> class.
23+
/// </summary>
24+
protected IdentityUserContext() { }
25+
}
26+
27+
/// <summary>
28+
/// Base class for the Entity Framework database context used for identity.
29+
/// </summary>
30+
/// <typeparam name="TUser">The type of user objects.</typeparam>
31+
/// <typeparam name="TKey">The type of the primary key for users and roles.</typeparam>
32+
public class IdentityUserContext<TUser, TKey> : IdentityUserContext<TUser, TKey, IdentityUserClaim<TKey>, IdentityUserLogin<TKey>, IdentityUserToken<TKey>>
33+
where TUser : IdentityUser<TKey>
34+
where TKey : IEquatable<TKey>
35+
{
36+
/// <summary>
37+
/// Initializes a new instance of the db context.
38+
/// </summary>
39+
/// <param name="options">The options to be used by a <see cref="DbContext"/>.</param>
40+
public IdentityUserContext(DbContextOptions options) : base(options) { }
41+
42+
/// <summary>
43+
/// Initializes a new instance of the class.
44+
/// </summary>
45+
protected IdentityUserContext() { }
46+
}
47+
48+
/// <summary>
49+
/// Base class for the Entity Framework database context used for identity.
50+
/// </summary>
51+
/// <typeparam name="TUser">The type of user objects.</typeparam>
52+
/// <typeparam name="TKey">The type of the primary key for users and roles.</typeparam>
53+
/// <typeparam name="TUserClaim">The type of the user claim object.</typeparam>
54+
/// <typeparam name="TUserLogin">The type of the user login object.</typeparam>
55+
/// <typeparam name="TUserToken">The type of the user token object.</typeparam>
56+
public abstract class IdentityUserContext<TUser, TKey, TUserClaim, TUserLogin, TUserToken> : DbContext
57+
where TUser : IdentityUser<TKey>
58+
where TKey : IEquatable<TKey>
59+
where TUserClaim : IdentityUserClaim<TKey>
60+
where TUserLogin : IdentityUserLogin<TKey>
61+
where TUserToken : IdentityUserToken<TKey>
62+
{
63+
/// <summary>
64+
/// Initializes a new instance of the class.
65+
/// </summary>
66+
/// <param name="options">The options to be used by a <see cref="DbContext"/>.</param>
67+
public IdentityUserContext(DbContextOptions options) : base(options) { }
68+
69+
/// <summary>
70+
/// Initializes a new instance of the class.
71+
/// </summary>
72+
protected IdentityUserContext() { }
73+
74+
/// <summary>
75+
/// Gets or sets the <see cref="DbSet{TEntity}"/> of Users.
76+
/// </summary>
77+
public DbSet<TUser> Users { get; set; }
78+
79+
/// <summary>
80+
/// Gets or sets the <see cref="DbSet{TEntity}"/> of User claims.
81+
/// </summary>
82+
public DbSet<TUserClaim> UserClaims { get; set; }
83+
84+
/// <summary>
85+
/// Gets or sets the <see cref="DbSet{TEntity}"/> of User logins.
86+
/// </summary>
87+
public DbSet<TUserLogin> UserLogins { get; set; }
88+
89+
/// <summary>
90+
/// Gets or sets the <see cref="DbSet{TEntity}"/> of User tokens.
91+
/// </summary>
92+
public DbSet<TUserToken> UserTokens { get; set; }
93+
94+
/// <summary>
95+
/// Configures the schema needed for the identity framework.
96+
/// </summary>
97+
/// <param name="builder">
98+
/// The builder being used to construct the model for this context.
99+
/// </param>
100+
protected override void OnModelCreating(ModelBuilder builder)
101+
{
102+
builder.Entity<TUser>(b =>
103+
{
104+
b.HasKey(u => u.Id);
105+
b.HasIndex(u => u.NormalizedUserName).HasName("UserNameIndex").IsUnique();
106+
b.HasIndex(u => u.NormalizedEmail).HasName("EmailIndex");
107+
b.ToTable("AspNetUsers");
108+
b.Property(u => u.ConcurrencyStamp).IsConcurrencyToken();
109+
110+
b.Property(u => u.UserName).HasMaxLength(256);
111+
b.Property(u => u.NormalizedUserName).HasMaxLength(256);
112+
b.Property(u => u.Email).HasMaxLength(256);
113+
b.Property(u => u.NormalizedEmail).HasMaxLength(256);
114+
115+
// Replace with b.HasMany<IdentityUserClaim>().
116+
b.HasMany<TUserClaim>().WithOne().HasForeignKey(uc => uc.UserId).IsRequired();
117+
b.HasMany<TUserLogin>().WithOne().HasForeignKey(ul => ul.UserId).IsRequired();
118+
b.HasMany<TUserToken>().WithOne().HasForeignKey(ut => ut.UserId).IsRequired();
119+
});
120+
121+
builder.Entity<TUserClaim>(b =>
122+
{
123+
b.HasKey(uc => uc.Id);
124+
b.ToTable("AspNetUserClaims");
125+
});
126+
127+
builder.Entity<TUserLogin>(b =>
128+
{
129+
b.HasKey(l => new { l.LoginProvider, l.ProviderKey });
130+
b.ToTable("AspNetUserLogins");
131+
});
132+
133+
builder.Entity<TUserToken>(b =>
134+
{
135+
b.HasKey(l => new { l.UserId, l.LoginProvider, l.Name });
136+
b.ToTable("AspNetUserTokens");
137+
});
138+
}
139+
}
140+
}

test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.InMemory.Test/InMemoryContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public InMemoryContext(DbContextOptions options) : base(options)
1414
}
1515

1616
public class InMemoryContext<TUser> :
17-
IdentityDbContext<TUser, string>
17+
IdentityUserContext<TUser, string>
1818
where TUser : IdentityUser
1919
{
2020
public InMemoryContext(DbContextOptions options) : base(options)

test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test/SqlStoreOnlyUsersTestBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected override bool ShouldSkipDbTests()
3232
return TestPlatformHelper.IsMono || !TestPlatformHelper.IsWindows;
3333
}
3434

35-
public class TestUserDbContext : IdentityDbContext<TUser, TKey>
35+
public class TestUserDbContext : IdentityUserContext<TUser, TKey>
3636
{
3737
public TestUserDbContext(DbContextOptions options) : base(options) { }
3838
}

0 commit comments

Comments
 (0)