EF核心查询自定义IdentiyUser

时间:2019-09-19 16:02:50

标签: c# ef-core-2.1

我对EF Core还是一个新手,之前我使用过PetaPoco,所以请原谅我的无知。在数据库中,我向AspNetUsers表添加了以下字段:

  • 高架
  • 已停用
  • 名字
  • 姓氏

然后我在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文件中也设置了AddIdentityAddScoped方法。

从现在开始,我不明白的是如何查询表,并返回我的自定义属性。在我的控制器中,我想执行以下操作:

/// <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);
}

但是显然我不能,因为用户DbSet上不存在这些属性。

长话短说,我认为我已经正确设置了所有内容,但实际上如何查询扩展的IdentityUser?

编辑

根据要求,以下是我的DbContext。我现在只连接了一个虚拟表,因为我只是想首先使AspNetUsers表上的扩展起作用:

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);
}

1 个答案:

答案 0 :(得分:1)

您必须为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);
}