How to save the same object in the database using entityFramework . net MVC?

Asked

Viewed 1,699 times

0

I need to update a table in the database using entityFramework, because when I update a value of a field and have it saved ,.

Please someone has a way to fix this??

grateful

   usuarioVM.ContatoUsuarios = ContatoUsuarioRepositorio.ObterContatoSemUsuario();

   //obtenho os meus usuarios que eu quero fazer o update         

foreach (var item in usuarioVM.ContatoUsuarios) //Lista dos meus objetos
        {
            var contatoUsuario = new ContatoUsuario() 
            {
                //Codigo=item.Codigo,
                CodigoUsuario = usuario.CodUsuario,
                Codigo=item.Codigo,//Adicionando os novos valores para as minhas propriedades do novo objeto
                CodTipoContato=item.CodTipoContato,
                Descricao=item.Descricao             
            };

            ContatoUsuarioRepositorio.Adicionar(contatoUsuario);//metodo do meu repositorio onde adiciono o objeto 
            ContatoUsuarioRepositorio.Commit();  //  metodo do meu repositorio "this.Context.SaveChanges();"          
        }


        public void Commit()
      {
         this.Context.SaveChanges();//Metodos utilizidados em outra classe mostrei aqui soh pra ter uma nocao
      }
        public void Adicionar(ContatoUsuario contato)
      {
        this.Context.ContatoUsuarios.Add(contato);
       }
  • 1

    Hello, which error gives when you try to save?

  • New transactions are not allowed because there are other threads running in the session

  • 3

    Could you please post some code so we can analyze?

  • 1

    And also the exception thrown.

1 answer

4

No need for methods Commit and Adicionar.

Also remove the SaveChanges from inside the loop.

Simply use:

usuarioVM.ContatoUsuarios = ContatoUsuarioRepositorio.ObterContatoSemUsuario();

//obtenho os meus usuarios que eu quero fazer o update         

foreach (var item in usuarioVM.ContatoUsuarios) //Lista dos meus objetos
{
    var contatoUsuario = new ContatoUsuario() 
    {
        CodigoUsuario = usuario.CodUsuario,
        Codigo = item.Codigo,//Adicionando os novos valores para as minhas propriedades do novo objeto
        CodTipoContato = item.CodTipoContato,
        Descricao = item.Descricao             
    };

    Context.ContatoUsuarios.Add(contato);      
}

Context.SaveChanges();
  • Show problem solved

  • 2

    @Hansmiller When an answer solves your problem, mark it as accepted... you did not accept any of the questions you asked, nor did you even vote for any of them. Thus, people will stop answering their questions, after all the stimulus they have (beyond the great level of altruism of these helper souls) after giving a correct and well-done answer, is to see that it has been accepted, and gain some reputation for it. Please... read the link How and why to accept an answer?

Browser other questions tagged

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