private async void ButtonDeletar_ClickAsync(object sender, RoutedEventArgs e)
        {
            MessageBoxResult resultado = MessageBox.Show("Tem certeza que deseja excluir o Cliente?", "Deletar Cliente", MessageBoxButton.YesNo, MessageBoxImage.Information);

            if (resultado == MessageBoxResult.Yes)
            {
                try
                {
                    using (AlexProva dbAlexProva = new AlexProva())
                    {
                        var clienteSelecionado = (Cliente)TabelaView.SelectedCells[0].Item;

                        Cliente clienteaDeletar = await dbAlexProva.Clientes.FindAsync(clienteSelecionado.Id);

                        dbAlexProva.Contatos.RemoveRange(clienteaDeletar.Contato); //Remove os contatos relacionados
                        dbAlexProva.Clientes.Remove(clienteaDeletar);              //Remove o cliente


                        await dbAlexProva.SaveChangesAsync();

                        MessageBox.Show("O Cliente foi Deletado", "Cliente Deletado", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Erro", MessageBoxButton.OK, MessageBoxImage.Error);
                }

                this.Hide();
                TelaPrincipalClientes telaPrincipalClientes = new TelaPrincipalClientes();
                telaPrincipalClientes.Show();
            }
        }
        private async void TextBusca_TextChangedAsync(object sender, TextChangedEventArgs e)
        {
            TabelaView.Items.Clear();
            try
            {
                using (AlexProva dBAlexProva = new AlexProva())
                {
                    List <Cliente> lista = new List <Cliente>();

                    lista = await dBAlexProva.Clientes.ToListAsync();

                    foreach (var x in lista)
                    {
                        if (x.Nome.Contains(TextBusca.Text))
                        {
                            TabelaView.Items.Add(x);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erro", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        private async void ButtonCadastrar_ClickAsync(object sender, RoutedEventArgs e)
        {
            try
            {
                if (fornecedorTabela == null)
                {
                    using (AlexProva dBAlexProva = new AlexProva())
                    {
                        var cliente = await dBAlexProva.Clientes.Where <Cliente>(x => x.Id == clienteTabela.Id).FirstOrDefaultAsync();

                        Contato contato = new Contato
                        {
                            Nome    = TextNome.Text.Trim(),
                            Numero  = TextNumTel.Text.Trim(),
                            Tipo    = TextTipo.Text.Trim(),
                            Cliente = cliente,
                        };

                        dBAlexProva.Contatos.Add(contato);
                        await dBAlexProva.SaveChangesAsync();
                    }
                    MessageBox.Show("Contato Cadastrado.", "Contato Cadastrado", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                {
                    using (AlexProva dBAlexProva = new AlexProva())
                    {
                        var fornecedor = await dBAlexProva.Fornecedores.Where <Fornecedor>(x => x.Id == fornecedorTabela.Id).FirstOrDefaultAsync();

                        Contato contato = new Contato
                        {
                            Nome       = TextNome.Text.Trim(),
                            Numero     = TextNumTel.Text.Trim(),
                            Tipo       = TextTipo.Text.Trim(),
                            Fornecedor = fornecedor,
                        };

                        dBAlexProva.Contatos.Add(contato);
                        await dBAlexProva.SaveChangesAsync();
                    }
                    MessageBox.Show("Contato Cadastrado.", "Contato Cadastrado", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erro", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            this.Close();

            if (fornecedorTabela == null)
            {
                TelaContatos telaContatos = new TelaContatos(clienteTabela);
                telaContatos.Show();
            }
            else
            {
                TelaContatos telaContatos = new TelaContatos(fornecedorTabela);
                telaContatos.Show();
            }
        }
        public async void LerContatosFornecedor(Fornecedor fornecedor)
        {
            try
            {
                using (AlexProva dBAlexProva = new AlexProva())
                {
                    var listaContatos = await dBAlexProva.Contatos.Where(x => x.Fornecedor.Id == fornecedor.Id).ToListAsync();

                    foreach (var x in listaContatos)
                    {
                        DataGridContatos.Items.Add(x);
                    }
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erro", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        public async void LerClientesAsync()
        {
            try
            {
                using (AlexProva dBAlexProva = new AlexProva())
                {
                    List <Cliente> lista = new List <Cliente>();

                    lista = await dBAlexProva.Clientes.ToListAsync();

                    foreach (var x in lista)
                    {
                        TabelaView.Items.Add(x);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erro", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        private async void ButtonDeletar_ClickAsync(object sender, RoutedEventArgs e)
        {
            try
            {
                this.Close();

                using (AlexProva dbAlexProva = new AlexProva())
                {
                    var idContato = (Contato)DataGridContatos.SelectedCells[0].Item;

                    var contatoSelecionado = await dbAlexProva.Contatos.Where(x => x.ContatoId == idContato.ContatoId).FirstOrDefaultAsync();

                    dbAlexProva.Contatos.Remove(contatoSelecionado);

                    await dbAlexProva.SaveChangesAsync();

                    MessageBox.Show("O Contato foi Deletado", "Contato Deletado", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erro", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        private async void ButtonCadastrar_ClickAsync(object sender, RoutedEventArgs e)
        {
            try
            {
                using (AlexProva dBalexProva = new AlexProva())
                {
                    if (clienteSelecionado == null) //Valida se é uma nova criação de cliente ou se é uma atualização
                    {
                        Endereco endereco = new Endereco
                        {
                            Rua         = TextRua.Text.Trim(),
                            Numero      = int.Parse(TextNumero.Text.Trim()),
                            Cep         = TextCEP.Text.Trim(),
                            Complemento = TextComplemento.Text.Trim(),
                            Bairro      = TextBairro.Text.Trim(),
                            Cidade      = TextCidade.Text.Trim(),
                            Estado      = TextEstado.Text.Trim(),
                        };

                        Contato contato = new Contato
                        {
                            Nome   = TextContatoNome.Text.Trim(),
                            Numero = TextContatoNum.Text.Trim(),
                            Tipo   = ComboboxTipo.Text.Trim(),
                        };

                        List <Contato> listContatos = new List <Contato>
                        {
                            contato
                        };

                        Cliente cliente = new Cliente
                        {
                            Nome     = TextNome.Text.ToString(),
                            Idade    = int.Parse(TextIdade.Text),
                            Endereco = endereco,
                            Contato  = listContatos,
                        };

                        dBalexProva.Clientes.Add(cliente);
                        await dBalexProva.SaveChangesAsync();


                        MessageBox.Show("Cliente Cadastrado.", "Cliente Cadastrado", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                    else
                    {
                        var clienteBanco = await dBalexProva.Clientes.Where <Cliente>(x => x.Id.ToString() == clienteSelecionado.Id.ToString()).FirstOrDefaultAsync();

                        clienteBanco.Nome                 = TextNome.Text.Trim();
                        clienteBanco.Idade                = int.Parse(TextIdade.Text.Trim());
                        clienteBanco.Endereco.Cep         = TextCEP.Text.Trim();
                        clienteBanco.Endereco.Rua         = TextRua.Text.Trim();
                        clienteBanco.Endereco.Numero      = int.Parse(TextNumero.Text.Trim());
                        clienteBanco.Endereco.Complemento = TextComplemento.Text.Trim();
                        clienteBanco.Endereco.Bairro      = TextBairro.Text.Trim();
                        clienteBanco.Endereco.Cidade      = TextCidade.Text.Trim();
                        clienteBanco.Endereco.Estado      = TextEstado.Text.Trim();

                        await dBalexProva.SaveChangesAsync();

                        MessageBox.Show("Cliente Alterado com sucesso", "Cliente Alterado", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erro", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            TelaPrincipalClientes TelaPrincipal = new TelaPrincipalClientes();

            TelaPrincipal.Show();
            this.Close();
        }
        private async void ButtonCadastrar_ClickAsync(object sender, RoutedEventArgs e)
        {
            try
            {
                using (AlexProva dBalexProva = new AlexProva())
                {
                    if (fornecedorSelecionado == null) //Valida se é uma nova criação do Fornecedor ou se é uma atualização
                    {
                        Endereco endereco = new Endereco
                        {
                            Rua         = TextRua.Text.Trim(),
                            Numero      = int.Parse(TextNumero.Text.Trim()),
                            Cep         = TextCEP.Text.Trim(),
                            Complemento = TextComplemento.Text.Trim(),
                            Bairro      = TextBairro.Text.Trim(),
                            Cidade      = TextCidade.Text.Trim(),
                            Estado      = TextEstado.Text.Trim(),
                        };

                        Contato contato = new Contato
                        {
                            Nome   = TextContatoNome.Text.Trim(),
                            Numero = TextContatoNum.Text.Trim(),
                            Tipo   = ComboboxTipo.Text.Trim(),
                        };

                        List <Contato> listContatos = new List <Contato>
                        {
                            contato
                        };

                        Fornecedor fornecedor = new Fornecedor
                        {
                            Nome         = TextNome.Text.ToString(),
                            PrazoEntrega = int.Parse(TextPrazoEntrega.Text.Trim()),
                            Endereco     = endereco,
                            Contato      = listContatos,
                        };

                        dBalexProva.Fornecedores.Add(fornecedor);
                        await dBalexProva.SaveChangesAsync();


                        MessageBox.Show("Fornecedor Cadastrado.", "Fornecedor Cadastrado", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                    else
                    {
                        var fornecedorBanco = await dBalexProva.Fornecedores.Where <Fornecedor>(x => x.Id.ToString() == fornecedorSelecionado.Id.ToString()).FirstOrDefaultAsync();

                        fornecedorBanco.Nome                 = TextNome.Text.Trim();
                        fornecedorBanco.PrazoEntrega         = int.Parse(TextPrazoEntrega.Text.Trim());
                        fornecedorBanco.Endereco.Cep         = TextCEP.Text.Trim();
                        fornecedorBanco.Endereco.Rua         = TextRua.Text.Trim();
                        fornecedorBanco.Endereco.Numero      = int.Parse(TextNumero.Text.Trim());
                        fornecedorBanco.Endereco.Complemento = TextComplemento.Text.Trim();
                        fornecedorBanco.Endereco.Bairro      = TextBairro.Text.Trim();
                        fornecedorBanco.Endereco.Cidade      = TextCidade.Text.Trim();
                        fornecedorBanco.Endereco.Estado      = TextEstado.Text.Trim();

                        await dBalexProva.SaveChangesAsync();

                        MessageBox.Show("Fornecedor alterado com sucesso", "Fornecedor Alterado", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erro", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            TelaPrincipalFornecedores telaPrincipalFornecedores = new TelaPrincipalFornecedores();

            telaPrincipalFornecedores.Show();
            this.Close();
        }