Write Telephone Array in the Database

2

I created a Clientes table and two from clients that are ClientesTelefone and ClientesEmail in my form I am generating email and phone fields dynamically, so I would like to know how I can receive and write these values in the tables , could you help me please ??

ApplicationClient : here I create the client.

public async Task<ApplicationClient> CreateClientAsync(ApplicationClient client)
    {
        try
        {
            var result = await _context.Clientes.AddAsync(client);

            if (result.State == EntityState.Added)
            {
                _context.SaveChanges();
            }

            return result.Entity;

        }
        catch (Exception)
        {

            throw;
        }
    }

ClientController:

if (ModelState.IsValid)
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                throw new ApplicationException($"Não é possível carregar o usuário com o ID '{_userManager.GetUserId(User)}'.");
            }

            var client = new ApplicationClient {
                TipoPessoa = model.TypePerson,Nome = model.Name,
                RazaoSocial = model.CompanyName, NomeFantasia = model.FantasyName,
                Cpfj = model.Cpfj, CEP = model.CEP, UF = model.UF,
                Bairro = model.Neighborhood, Cidade = model.City,
                Endereco = model.Address, Numero = model.Number,
                Complemento = model.Complement, InfoAdicionais = model.AddInformation,
                DataNascimento = model.BirthDate, UsuarioId = user.Id
            };

            var result = await _clientManager.CreateClientAsync(client);

            var clientTelephone = new ApplicationClientTelephone
            {
                Telefone = model.Telephone

            };

            var clientEmail = new ApplicationClientEmail
            {
                Email = model.Email
            };

            TempData["MensagemSucesso"] = "Cliente cadastrado com sucesso";

            return View("Index");
        }

ApplicationClient:

public class ApplicationClient
{
    [Key]
    public Guid Id { get; set; }

    public String Nome { get; set; }

    public Guid UsuarioId { get; set; }

    public virtual ICollection<ApplicationClientTelephone> ClientesTelefone { get; set; }

    public virtual ICollection<ApplicationClientEmail> ClientesEmail { get; set; }

    [ForeignKey("UsuarioId")]
    public virtual ApplicationUser Usuario { get; set; }

}

I have ApplicationClientTelephone E ApplicationClientEmail .

I wish that after the customer was added, I would start to generate a chain reaction that would add the customer's phone numbers and emails.

If you can help me, thank you very much.

    
asked by anonymous 18.09.2018 / 21:54

1 answer

0

Before this Call add the phones and emails in the list

var result = await _clientManager.CreateClientAsync(client);

The code should stay close to that

clientTelephone.ClientesTelefone = new List<ApplicationClientTelephone>(){
   new ApplicationClientTelephone
   {
      Telefone = model.Telephone
   }
};

clientTelephone.ClientesEmail = new List<ApplicationClientEmail>(){
   new ApplicationClientEmail
   {
      Email = model.Email
   }
};

var result = await _clientManager.CreateClientAsync(client);

If you want to add more than one email or phone, you must make a for and call the Add () method of the lists to add the items, assuming model.Email and model.Telephone are lists.

clientTelephone.ClientesTelefone = new List<ApplicationClientTelephone>();
clientTelephone.ClientesEmail = new List<ApplicationClientEmail>();

foreach(var telefone in model.Telefone)
{
    clientTelephone.ClientesTelefone.Add(new ApplicationClientTelephone() { Telefone = telefone });
}

foreach(var email in model.Email)
{
    clientTelephone.ClientesEmail.Add(new ApplicationClientEmail() { Email = email });
}

var result = await _clientManager.CreateClientAsync(client);
    
18.09.2018 / 22:12