将具有身份的集合添加到ASP.NET Core中的用户对象

时间:2018-09-25 16:24:25

标签: c# asp.net-core entity-framework-core asp.net-core-identity

我在玩EF Core和ASP.NET Core,偶然发现了以下问题。 我想以列表的形式向用户对象添加一些额外的数据。问题在于列表永远不会更新。

这是我的DbContext:

public class ApplicationDbContext : IdentityDbContext<HostUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
    {
    }
}

现在是我的用户对象:

public class HostUser : IdentityUser
{
    [PersonalData]
    public ICollection<GuestUser> GuestUsers { get; set; }
}

这是在Contoller中添加新的GuestUser:

[HttpPost]
    public async Task<IActionResult> Post([FromBody]GuestUser userToInsert)
    {
        if (userToInsert == null)
        {
            return BadRequest();
        }

        var currentUser = await GetCurrentUserAsync();
        if (currentUser == null)
        {
            return Forbid();
        }

        if(currentUser.GuestUsers?.Any(user => user.Id == userToInsert.Id) ?? false)
        {
            return BadRequest();
        }

        if(currentUser.GuestUsers == null)
        {
            currentUser.GuestUsers = new List<GuestUser>();
        }

        currentUser.GuestUsers.Add(userToInsert);
        await userManager.UpdateAsync(currentUser);

        return Ok();
    }

我的问题是,这是否是完全错误的方法,我必须在DbContext中添加GuestUser的DbSet并将其映射到用户。 如果是这种情况,我不知道如何实现。 注意:在这种情况下,GuestUser不是另一个IdentityUser,而是本地用户数据

2 个答案:

答案 0 :(得分:2)

这可能是这样的:

实体:

public class HostUser : IdentityUser
{
    public virtual ICollection<GuestUser> GuestUsers { get; set; }
}

public class GuestUser
{
    public int HostUserId { get; set; }
    public virtual HostUser HostUser { get; set; }
}

数据库上下文:

public class ApplicationDbContext : IdentityDbContext<HostUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    public DbSet<GuestUser> GuestUsers { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<HostUser>(
            typeBuilder =>
            {
                typeBuilder.HasMany(host => host.GuestUsers)
                    .WithOne(guest => guest.HostUser)
                    .HasForeignKey(guest => guest.HostUserId)
                    .IsRequired();

                // ... other configuration is needed
            });

        builder.Entity<GuestUser>(
            typeBuilder =>
            {
                typeBuilder.HasOne(guest => guest.HostUser)
                    .WithMany(host => host.GuestUsers)
                    .HasForeignKey(guest => guest.HostUserId)
                    .IsRequired();

                // ... other configuration is needed
            });
    }
}

控制器动作:

[HttpPost]
public async Task<IActionResult> Post([FromBody] GuestUser userToInsert)
{
    // All checks and validation ...

    // You can get the current user ID from the user claim for instance
    int currentUserId = int.Parse(User.FindFirst(Claims.UserId).Value);

    // _context is your ApplicationDbContext injected via controller constructor DI 
    userToInsert.HostUserId = currentUserId;

    // Save new guest user associated with the current host user
    _context.GuestUsers.Add(userToInsert);
    await _context.SaveChangesAsync();

    // If you need to get the current user with all guests
    List<HostUser> currentUser = await _context.Users.Include(host => host.GuestUsers).ToListAsync();

    return Ok(currentUser);
}

这里提供了完整的代码-definition(所有需要的自定义类都继承自IdentityDbContext使用的基类)。

答案 1 :(得分:1)

有两个问题。

1。首先,您应该将ViewModel与实体分开(不要通过GuestUser作为参数的web / api方法)

2。然后,如您提到的,您应该在DBSet中声明GuestUser中的DbContext,并将其映射到用户表。

  

定义您的自定义用户实体

public class ApplicationUser : IdentityUser
{
    public string CustomTag { get; set; }
}
  

声明您的自定义DbContext

     

接下来,将此类型用作上下文的通用参数:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<ApplicationUser>(b =>
        {
            // Each User can have many UserClaims
            b.HasMany(e => e.Claims)
                .WithOne()
                .HasForeignKey(uc => uc.UserId)
                .IsRequired();
        });
    }
}
  

更新ConfigureServices以使用新的ApplicationUser类:

services.AddDefaultIdentity<ApplicationUser>()
    .AddEntityFrameworkStores<ApplicationDbContext>();

如果还有其他问题,请告诉我。