// 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.
///
public class IdentityDbContext : IdentityDbContext
{
///
/// Initializes a new instance of .
///
/// 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.
///
/// The type of the user objects.
public class IdentityDbContext : IdentityDbContext where TUser : IdentityUser
{
///
/// Initializes a new instance of .
///
/// 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.
///
/// 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.
///
/// The type of user objects.
/// The type of role objects.
/// The type of the primary key for users and roles.
public class IdentityDbContext : IdentityDbContext, IdentityUserRole, IdentityUserLogin, IdentityRoleClaim, IdentityUserToken>
where TUser : IdentityUser
where TRole : IdentityRole
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.
///
/// 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.
///
/// The type of user objects.
/// The type of role objects.
/// The type of the primary key for users and roles.
/// The type of the user claim object.
/// The type of the user role object.
/// 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
where TUser : IdentityUser
where TRole : IdentityRole
where TKey : IEquatable
where TUserClaim : IdentityUserClaim
where TUserRole : IdentityUserRole
where TUserLogin : IdentityUserLogin
where TRoleClaim : IdentityRoleClaim
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 User roles.
///
public DbSet UserRoles { get; set; }
///
/// Gets or sets the of roles.
///
public DbSet Roles { get; set; }
///
/// Gets or sets the of role claims.
///
public DbSet RoleClaims { 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)
{
base.OnModelCreating(builder);
builder.Entity(b =>
{
b.HasMany().WithOne().HasForeignKey(ur => ur.UserId).IsRequired();
});
builder.Entity(b =>
{
b.HasKey(r => r.Id);
b.HasIndex(r => r.NormalizedName).HasName("RoleNameIndex").IsUnique();
b.ToTable("AspNetRoles");
b.Property(r => r.ConcurrencyStamp).IsConcurrencyToken();
b.Property(u => u.Name).HasMaxLength(256);
b.Property(u => u.NormalizedName).HasMaxLength(256);
b.HasMany().WithOne().HasForeignKey(ur => ur.RoleId).IsRequired();
b.HasMany().WithOne().HasForeignKey(rc => rc.RoleId).IsRequired();
});
builder.Entity(b =>
{
b.HasKey(rc => rc.Id);
b.ToTable("AspNetRoleClaims");
});
builder.Entity(b =>
{
b.HasKey(r => new { r.UserId, r.RoleId });
b.ToTable("AspNetUserRoles");
});
}
}
}