EF Core 查询自定义IdentiyUser
EF Core Query Custom IdentiyUser
我对EF Core还是比较陌生,之前用过PetaPoco,所以请原谅我的无知。在我的数据库中,我将以下字段添加到我的 AspNetUsers table:
- 提升
- 已停用
- 名字
- 姓氏
然后我在 this 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; }
}
和
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;
}
}
我还在 Startup.cs 文件中设置了 AddIdentity
和 AddScoped
方法。
从现在开始我不明白的是如何查询 table,返回我的自定义属性。在我的控制器中,我想做这样的事情:
/// <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);
}
但显然我不能,因为 Users DbSet 上不存在这些属性。
长话短说,我认为我已经正确设置了所有内容,但我如何实际查询我的扩展 IdentityUser?
编辑
应要求,以下是我的DbContext。我现在只有一个虚拟 table 接线,因为我只是试图让 AspNetUsers table 上的扩展首先工作:
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);
}
您必须为 IdentityDbContext
指定类型参数。您需要继承自 IdentityDbContext<TUser>
class。如果没有类型参数,您将使用 ASP.NET Core Identity 中定义的 类。您想要更改的所有内容都必须反映在您的代码中。
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);
}
我对EF Core还是比较陌生,之前用过PetaPoco,所以请原谅我的无知。在我的数据库中,我将以下字段添加到我的 AspNetUsers table:
- 提升
- 已停用
- 名字
- 姓氏
然后我在 this 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; }
}
和
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;
}
}
我还在 Startup.cs 文件中设置了 AddIdentity
和 AddScoped
方法。
从现在开始我不明白的是如何查询 table,返回我的自定义属性。在我的控制器中,我想做这样的事情:
/// <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);
}
但显然我不能,因为 Users DbSet 上不存在这些属性。
长话短说,我认为我已经正确设置了所有内容,但我如何实际查询我的扩展 IdentityUser?
编辑
应要求,以下是我的DbContext。我现在只有一个虚拟 table 接线,因为我只是试图让 AspNetUsers table 上的扩展首先工作:
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);
}
您必须为 IdentityDbContext
指定类型参数。您需要继承自 IdentityDbContext<TUser>
class。如果没有类型参数,您将使用 ASP.NET Core Identity 中定义的 类。您想要更改的所有内容都必须反映在您的代码中。
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);
}