Return customer data [closed]

-2

Good morning, people,

I would like to create a function, which returns the requested client, and its data, which are in 3 different tables Clientes , ClientesTelephone , ClientesEmail , I can add the client perfectly with my% / p>

CreateClientAsync:

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;
        }
    }

I am trying to develop a function to return client data, called CreateClientAsync , but I do not know where to start, could you help me please?

public async Task<ApplicationClient> GetClientAsync(ApplicationClient client)
    {
        try
        {


        }
        catch (Exception)
        {

            throw;
        }
    }

Table GetClientAsync :

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

    [Required]
    public int TipoPessoa { 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; }

}'

Tables Clientes :

public class ApplicationClientEmail
{
    [Key]
    public long Id { get; set; }

    public String Email { get; set; }

    public Guid ClienteId { get; set; }

    [ForeignKey("ClienteId")]
    public virtual ApplicationClient Cliente { get; set; }
}

Tables ClientesEmail :

public class ApplicationClientTelephone
{
    [Key]
    public long Id { get; set; }

    public String Telefone { get; set; }

    public Guid ClienteId { get; set; }

    [ForeignKey("ClienteId")]
    public virtual ApplicationClient Cliente { get; set; }
}
    
asked by anonymous 20.09.2018 / 14:05

1 answer

1

You must use Include if the LazyLoading option is set to false, otherwise it will automatically do the Joins.

Follow the example below:

var result = await _context.Clientes.Include("ClientEmail").Include("ClientTelephone").FirstOrDefaultAsync(m => m.ID == id);

I recommend using FirstOrDefault to not generate an exception if the element does not exist, it will only return null, more about: link

    
20.09.2018 / 15:07