Return data from the Customer

Asked

Viewed 73 times

-2

Good morning Personal,

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 function CreateClientAsync.

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 customer data, call GetClientAsync, but I don’t know where to start, could help me please ?

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


        }
        catch (Exception)
        {

            throw;
        }
    }

Table Clientes:

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 ClientesEmail:

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 ClientesTelefone:

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

1 answer

1


You must use the Include (EF Join) if option Lazyloading 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 not to generate an exception if the element does not exist, it will only return null, more about: https://stackoverflow.com/questions/1024559/when-to-use-first-and-when-to-use-firstordefault-with-linq

  • I’m getting the error that 'Applicationclient' does not contain a definition for 'Getawaiter'

  • Sorry, change the Firstordefault for Firstordefaultasync

  • I’m using this function to check if the CNPJ or CPF , is already inserted in the table, so from what I understand, if it does not find it will return null, and from that null can make a if and make that check, correct ?

  • That! exactly, you will test if the Client object is different from null and treat, if(client != null) { // Found! } Else { //Did not find }

  • In case I can’t get back to var result, 'cause I get the following mistake: Não é possível converter implicitamente tipo "EasySistema.Models.ApplicationClient em Microsoft.Asp.NetCore.Mvc.ActionResult

Browser other questions tagged

You are not signed in. Login or sign up in order to post.