将 Collection 添加到 ASP.NET Core with Identity 中的用户对象
Add Collection to user object in ASP.NET Core with 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; }
}
这里是在控制器中添加一个新的 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,它是本地用户数据
有两个问题。
1.First 您应该将 ViewModel
与实体分开(不要将 GuestUser
作为参数的 web/api 方法传递)
2.then 正如您提到的,您应该在 DbContext
中声明 GuestUser
的 DBSet
并将其映射到用户 table.
Define your Custom User Entity
public class ApplicationUser : IdentityUser
{
public string CustomTag { get; set; }
}
Declare your Custom DbContext
Next use this type as a generic argument for the context:
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();
});
}
}
Update ConfigureServices to use the new ApplicationUser class:
services.AddDefaultIdentity<ApplicationUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
如果您有任何其他问题,请告诉我。
它可能是这样的:
实体:
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);
}
我在这里提供了完整的代码 - (所有需要的自定义类继承自 IdentityDbContext
使用的基础 类)。
我在使用 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; }
}
这里是在控制器中添加一个新的 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,它是本地用户数据
有两个问题。
1.First 您应该将 ViewModel
与实体分开(不要将 GuestUser
作为参数的 web/api 方法传递)
2.then 正如您提到的,您应该在 DbContext
中声明 GuestUser
的 DBSet
并将其映射到用户 table.
Define your Custom User Entity
public class ApplicationUser : IdentityUser
{
public string CustomTag { get; set; }
}
Declare your Custom DbContext
Next use this type as a generic argument for the context:
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();
});
}
}
Update ConfigureServices to use the new ApplicationUser class:
services.AddDefaultIdentity<ApplicationUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
如果您有任何其他问题,请告诉我。
它可能是这样的:
实体:
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);
}
我在这里提供了完整的代码 - IdentityDbContext
使用的基础 类)。