8
How do I handle duplicate key error? I need to display message to the user that the "Item is already registered"
Something like that:
try{
  //tenta inserir no Banco de Dados
  context.Produto.Add(_produto);
  context.SaveChanges();
}catch(exceção 1){
  //tratar erro de chave duplicada
}catch(exceção 2){
  // tratar erro diferente de chave duplicada
} 
Error always returns in class catch(Exception ex)
        _repositorio.ProdutoEstoque.Add(_produtoEstoque);
        _repositorio.SaveChanges();
    }
    catch (UpdateException ex)
    {
        SqlException innerException = ex.InnerException as SqlException;
        if (innerException != null && innerException.Number == 2627){                    
            //tratar erro de chave duplicada
            //o jeito que vai informar a view depende do resto da sua aplicação
            string msg = string.Empty;
            msg ="ERRO";
        }else
        {
            throw;
        }
    }
    catch (SqlException ex){
        if (ex.Number == 2627){
            string msg = string.Empty;
            msg = "ERRO";
        }else
        {
            throw;
        }
    }
    catch (Exception ex){  
         throw;               
    }
}

How about doing a query first? If you are saving records in multiple tables at the same time, you may not know where such duplication exists.
– Alberto Cláudio Mandlate