Search code examples
c#entity-frameworkentity-framework-4.1many-to-manyentity-relationship

Entity Framework Insert many-to-many database first


    pessoas objPessoa;
DataTable dtTelefones;
DataTable dtEnderecos;
private void salvar()
{
    using (EarthWorkEntities ctx = new EarthWorkEntities())
    {
        frame2objeto();
        ctx.AddTopessoas(objPessoa);
        ctx.SaveChanges();
    }
}


private void frame2objeto()
        {
            using (EarthWorkEntities ctx = new EarthWorkEntities())
            {
                objPessoa = new pessoas();
                //Pessoa
                objPessoa.pes_cpfcnpj = textCpf.Text;
                objPessoa.pes_nome = textNome.Text;
                objPessoa.pes_sexo = cbSexo.SelectedIndex == 0 ? "M" : "F";

                //Cliente
                clientes cliente = new clientes();
                cliente.cli_dt_ultima = DateTime.Now;
                objPessoa.clientes.Add(cliente);
                //Telefones
                foreach (DataRow row in dtTelefones.Rows)
                {
                    telefones objTelefones;
                    objTelefones = new telefones();
                    objTelefones.EntityKey = null;
                    objTelefones.tel_contato = row["Contato"].ToString();
                    objTelefones.tel_ddd = row["DDD"].ToString();
                    objTelefones.tel_numero = row["Numero"].ToString();
                    string tipo = row["Tipo"].ToString();
                    tipos_telefones tipo_telefone = (from t in ctx.tipos_telefones
                                         where t.tptel_descr == tipo
                                         select t).FirstOrDefault<tipos_telefones>();
                    objTelefones.tipos_telefones = tipo_telefone;
                    objPessoa.telefones.Add(objTelefones);
                }
                //Endereço
                foreach (DataRow row in dtEnderecos.Rows)
                {
                    enderecos objEnderecos;
                    objEnderecos = new enderecos();
                    objEnderecos.EntityKey = null;
                    objEnderecos.end_bairro = row["Bairro"].ToString();
                    objEnderecos.end_cep = row["CEP"].ToString();
                    objEnderecos.end_cidade = row["Cidade"].ToString();
                    objEnderecos.end_complemento = row["Complemento"].ToString();
                    objEnderecos.end_logradouro = row["Logradouro"].ToString();
                    objEnderecos.end_numero = row["Numero"].ToString();
                    string tipo = row["Tipo"].ToString();
                    tipos_enderecos tipo_endereco = (from t in ctx.tipos_enderecos
                                         where t.tpend_descr == tipo
                                         select t).FirstOrDefault<tipos_enderecos>();
                    objEnderecos.tipos_enderecos = tipo_endereco;
                    objPessoa.enderecos.Add(objEnderecos);
                }
            }
        }

When running the command "AddTopessoas ()" returns the following error: "The EntityKey property can only be set when the current value of the property is null."

Tables: pessoas >> pessoas_telefones << telefones pessoas >> pessoas_enderecos << enderecos


Solution

  • I didn't quite understand your code. But I did notice that you are not calling SaveChanges() inside frame2objeto(), so once you go out of using block, they were discarded.

    Even though you DID save changes, when you return from it to your salvar(), you wouldn't be seeing any changes since it is a separate context altogether.