Outline

Demo: Complete Create Account

In the Conduit.Repositories Project

Modify AccountRepository.cs file
  • Create variable called result
  • Assign the value of Context.Accounts.AddAsync
  • Provide the parameter new Account
    • Map the values from the register object to the new Account
    • Map the new Person nested object with from values from the register object
  • Add the ConfigureAwait function to prevent the process from holding any threads.
Save the changes to the Context Object
  • Call the SaveChangesAsync on the Context object
  • Append the await keyword
  • Add the ConfigureAwait function to prevent the process from holding any threads.
Return the expected result
  • Return the Entity property from the result
Fix errors
  • Hover over the errors that are displaying
  • Make the method async

Here's the code the Conduit.Repositories.AccountRepository.CreateAccountAsync method:

        private async Task<Account> CreateAccountAsync(Register register)
        {
            byte[] passwordHash, passwordSalt;
            CreatePasswordHash(register.Password, out passwordHash, out passwordSalt);
            var result = await Context.Accounts.AddAsync(new Account
            {
                Email = register.Email,
                PasswordHash = passwordHash,
                PasswordSalt = passwordSalt,
                Person = new Person
                {
                    UserName = register.UserName
                }
            }).ConfigureAwait(false);
            await Context.SaveChangesAsync().ConfigureAwait(false);
            return result.Entity;
        }