我正在玩EF Core和ASP.NET Core,偶然发现了以下问题。我想以List的形式向User Object添加一些额外的数据。问题是列表永远不会更新。
这是我的DbContext:
public class ApplicationDbContext : IdentityDbContext<HostUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
}
现在我的用户对象:
public class HostUser : IdentityUser
{
[PersonalData]
public ICollection<GuestUser> GuestUsers { get; set; }
}
这里是在Controller中添加新的访客用户:
[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中添加一个GuestStser的DbSet并将其映射到用户。如果是这种情况我不知道如何实现这一目标。注意:在这种情况下,GuestUser不是另一个IdentityUser,它是本地用户数据
这是它的样子:
实体:
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; }
}
DB上下文:
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);
}
在这里,我提供了完整的代码 - how to configure custom Identity based DB context(所有需要的自定义分类继承自IdentityDbContext
使用的基类)。
有两个问题。
1.首先你应该分开ViewModel
from实体(不要传递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>();
如果您有任何其他问题,请告诉我。