Я использую DotNet Core Identity для создания нового пользователя. После успешного создания пользователя я хочу сохранить UserId и CourseId (ссылка на внешний ключ) в другую таблицу.
My IdentityDBContext Class
public class AppIdentityDbContext : IdentityDbContext<AppUser, AppRole, Guid>
{
public AppIdentityDbContext(DbContextOptions<AppIdentityDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
builder.Entity<AppUser>().ToTable("User");
builder.Entity<IdentityUserRole<Guid>>().ToTable("UserRole");
builder.Entity<IdentityUserLogin<Guid>>().ToTable("UserLogin");
builder.Entity<IdentityUserClaim<Guid>>().ToTable("UserClaim");
builder.Entity<AppRole>().ToTable("Role");
builder.Entity<IdentityUserToken<Guid>>().ToTable("UserToken");
builder.Entity<IdentityRoleClaim<Guid>>().ToTable("RoleClaim");
}
}
Способ регистрации пользователя
public async Task<OperationStatus> Signup(SignupViewModel model)
{
OperationStatus ObjStatus = new OperationStatus();
try
{
var user = new AppUser
{
FirstName = model.FirstName.Trim(),
LastName = model.LastName.Trim(),
UserName = model.Email.Trim(),
Email = model.Email.Trim(),
PhoneNumber = model.PhoneNumber.Trim(),
ModifiedDate = DateTime.UtcNow
};
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
// I want to do somewhat like this
// --------------------------------------------- --------------------
UserCourse userCourse = new UserCourse {
UserId = user.Id,
CourseId = model.CourseId
};
context.UserCourses.Add(userCourse);
context.SaveChanges();
// --------------------------------------------- -------------------
ObjStatus.IsSuccess = true;
ObjStatus.Message = "Registration Successful";
}
else
{
ObjStatus.IsSuccess = false;
ObjStatus.Message = "Unable to process your request.";
}
}
catch (Exception ex)
{
throw ex;
}
return ObjStatus;
}