I am still relatively new to EF Core and beforehand I used PetaPoco, so please forgive my ignorance. In my database, I added the following fields to my AspNetUsers table:
I then created the following classes followingthis blog article :
public partial class ApplicationUser : IdentityUser
{
public bool? Deactivated { get; set; }
public bool? Elevated { get; set; }
[StringLength(255)]
public string FirstName { get; set; }
[StringLength(255)]
public string LastName { get; set; }
}
and
public class ApplicationClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser, IdentityRole>
{
public ApplicationClaimsPrincipalFactory(UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager, IOptions<IdentityOptions> optionsAccessor) : base(userManager, roleManager, optionsAccessor)
{ }
public override async Task<ClaimsPrincipal> CreateAsync(ApplicationUser user)
{
var principal = await base.CreateAsync(user);
if (!string.IsNullOrWhiteSpace(user.FirstName))
{
((ClaimsIdentity)principal.Identity).AddClaims(new[] {
new Claim(ClaimTypes.GivenName, user.FirstName)
});
}
if (!string.IsNullOrWhiteSpace(user.LastName))
{
((ClaimsIdentity)principal.Identity).AddClaims(new[] {
new Claim(ClaimTypes.Surname, user.LastName)
});
}
return principal;
}
}
I also setup the AddIdentity
and AddScoped
methods too in my Startup.cs file.
What I do not understand going forward from this point is how to query the table, returning my custom properties. In my controller, I want to do something like this:
/// <summary>
/// Gets every User.
/// </summary>
/// <returns>HTTP Result</returns>
[HttpGet]
[Route("")]
public async Task<ActionResult<IEnumerable<ApplicationUser>>> GetUsers()
{
var users = await this._context.Users.Select(user => new
{
user.Id,
user.Decativated,
user.Elevated,
user.Email,
user.FirstName,
user.LastName
}).ToListAsync();
return Ok(users);
}
But obviously I can't because the properties don't exist on the Users DbSet.
So long story short, I think that I have everything setup properly but how do I actually query my extended IdentityUser?
EDIT
As requested, the following is my DbContext. I only have a dummy table wired up right now as I was just trying to get the extensions on the AspNetUsers table working first:
public partial class [removed for confidentiality]Context : IdentityDbContext
{
public [removed for confidentiality]Context()
{
}
public [removed for confidentiality]Context(DbContextOptions<[removed for confidentiality]Context> options)
: base(options)
{
}
public virtual DbSet<Foo> Foos { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasAnnotation("ProductVersion", "[removed for confidentiality]");
modelBuilder.Entity<Foo>(entity =>
{
entity.Property(e => e.FooName).IsUnicode(false);
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
You have to specify type parameter(s) for IdentityDbContext
. You need to inherit from IdentityDbContext<TUser>
class . Without type parameters you are using classes defined in ASP.NET Core Identity. Everything you'd like to change must be reflected in your code.
public partial class [removed for confidentiality]Context : IdentityDbContext<ApplicationUser>
{
public [removed for confidentiality]Context()
{
}
public [removed for confidentiality]Context(DbContextOptions<[removed for confidentiality]Context> options)
: base(options)
{
}
public virtual DbSet<Foo> Foos { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasAnnotation("ProductVersion", "[removed for confidentiality]");
modelBuilder.Entity<Foo>(entity =>
{
entity.Property(e => e.FooName).IsUnicode(false);
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.