I have these models in ASP.NET Core:
public abstract class EntityBase
{
[Key]
public int Id { get; set; }
}
public class User : EntityBase
{
public string Username { get; set; }
public string Password { get; set; }
public virtual ICollection<Merchant> Merchants { get; set; }
}
public class Merchant : EntityBase
{
public string MerchantName { get; set; }
public string AccountNumber { get; set; }
public int? UserId { get; set; }
[ForeignKey("UserId")]
public virtual User User { get; set; }
public virtual ICollection<Mandate> Mandates { get; set; }
}
ViewModel (DTO):
public class RegisterDto
{
public string MerchantName { get; set; }
[Required(ErrorMessage = "Account Number is required")]
[MaxLength(50)]
public string AccountNumber { get; set; }
[MaxLength(100)]
[Required(ErrorMessage = "Username is required")]
[RegularExpression(@"^\S*$", ErrorMessage = "No white space allowed")]
public string Username { get; set; }
[MaxLength(128)]
[Required(ErrorMessage = "Password is required")]
public string Password { get; set; }
}
EntityMapper:
public class EntityMapper
{
public User FromRegisterDtoToUser(RegisterDto register)
{
User user = new User()
{
Username = register.Username,
Password = register.Password,
};
return user;
}
public Merchant FromRegisterDtoToMerchant(RegisterDto register)
{
Merchant merchant = new Merchant()
{
MerchantName = register.MerchantName,
AccountNumber = register.AccountNumber,
UserId = ?
};
return merchant;
}
}
AuthService:
public async Task<UserDto> register(RegisterDto register)
{
var userExists = await _unitOfWork.UserRepository.GetByUsername(register.Username);
if (userExists != null)
{
throw new Exception("User already exists!");
}
else
{
try
{
register.Password = User.ComputeSha256Hash(register.Password);
var mapper = new EntityMapper();
var user = mapper.FromRegisterDtoToUser(register);
var merchant = mapper.FromRegisterDtoToMerchant(register);
await _unitOfWork.UserRepository.Insert(user);
await _unitOfWork.MerchantRepository.Insert(merchant);
await _unitOfWork.SaveChangesAsync();
if (user != null)
{
var token = GetToken(user);
var map = new EntityMapper();
return map.FromUserToUserDto(user, token);
}
return null;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
}
UserId in Merchant is the Foreign key for Id in User. Id is autogenerated.
Since both User and Merchant Data are inserted at the same time, I want to automatically insert UserId as the Id generated from User in:
Merchant merchant = new Merchant()
{
MerchantName = register.MerchantName,
AccountNumber = register.AccountNumber,
UserId = ...?
};
How do I achive this?
Thanks
CodePudding user response:
I'm not sure why database technology/framework you are using (I'm assuming Entity Framework) but if the UserId
is generated upon database insert you will need to save the changes to the database after inserting user and do it again when you insert the merchant.
Basically
await _unitOfWork.UserRepository.Insert(user);
await _unitOfWork.SaveChangesAsync(); // user should have an id now.
merchant.UserId = user.Id;
await _unitOfWork.MerchantRepository.Insert(merchant);
await _unitOfWork.SaveChangesAsync();