9
My domain:
public class SBE_ST_CorpoDocente
{
public int Id { get; set; }
public string Nome { get; set; }
public virtual ICollection<SBE_ST_Curso> Cursos { get; set; }
}
public class SBE_ST_Curso
{
public int Id { get; set; }
public string Titulo { get; set; }
public virtual ICollection<SBE_ST_CorpoDocente> Coordenacao { get; set; }
}
Layer of interaction with the bank:
public void Salvar(SBE_ST_Curso entidade)
{
var idsCoordenacao = entidade.Coordenacao.Select(c => c.Id).ToList();
var coordenacao = contexto.CorpoDocente.Where(cd => idsCoordenacao.Contains(cd.Id)).ToList();
if (entidade.Id > 0)
{
var cursoAlterar = contexto.Curso.First(x => x.Id == entidade.Id);
cursoAlterar.Titulo = entidade.Titulo;
cursoAlterar.Coordenacao = coordenacao;
contexto.Entry(cursoAlterar).State = EntityState.Modified;
contexto.SaveChanges();
}
else
{
entidade.Coordenacao = coordenacao;
contexto.Curso.Add(entidade);
contexto.SaveChanges();
}
When I enter a new record it works perfectly. When I update a record it gives error. If I clean the table that stores the relationships, then I can edit normal the first time, then gives the same error.
Error:
Additional information: An error occurred while saving entities that do not Expose Foreign key properties for their relationships. The Entityentries Property will Return null because a single Entity cannot be identified as the source of the Exception. Handling of exceptions while saving can be made easier by exposing Foreign key properties in your Entity types. See the Innerexception for Details.
Analyzing the Intellitrace:
Exception:Caught: "Violation of PRIMARY KEY Constraint 'Pk_dbo.Sbe_st_cursosbe_st_corpodocente'. Cannot Insert Duplicate key in Object 'dbo.Sbe_st_cursosbe_st_corpodocente'. The Duplicate key value is (8, 1). The statement has been terminated." (System.Data.Sqlclient.Sqlexception) A System.Data.Sqlclient.Sqlexception was Caught: "Violation of PRIMARY KEY Constraint 'Pk_dbo.Sbe_st_cursosbe_st_corpodocente'. Cannot Insert Dcate uplikey in Object 'dbo.Sbe_st_cursosbe_st_corpodocente'. The Duplicate key value is (8, 1). The statement has been terminated." Time: 11/08/2014 15:28:19 Thread:Worker Thread[3508]
I made a GAMBIARRA to solve this problem. (GAMBIARRA REALLY)
public void LimparRelacionamentos(int id)
{
SqlConnection MinhaConexao = new SqlConnection(ConfigurationManager.ConnectionStrings["BancoDados"].ConnectionString);
MinhaConexao.Open();
string query = "DELETE FROM SBE_ST_CursoSBE_ST_CorpoDocente WHERE SBE_ST_Curso_Id = " + id;
SqlCommand comando = new SqlCommand(query, MinhaConexao);
comando.ExecuteNonQuery();
MinhaConexao.Close();
}
public void Salvar(SBE_ST_Curso entidade)
{
var idsCoordenacao = entidade.Coordenacao.Select(c => c.Id).ToList();
var coordenacao = contexto.CorpoDocente.Where(cd => idsCoordenacao.Contains(cd.Id)).ToList();
if (entidade.Id > 0)
{
var cursoAlterar = contexto.Curso.First(x => x.Id == entidade.Id);
cursoAlterar.Titulo = entidade.Titulo;
LimparRelacionamentos(entidade.Id);
cursoAlterar.Coordenacao = coordenacao;
contexto.SaveChanges();
}
else
{
entidade.Coordenacao = coordenacao;
contexto.Curso.Add(entidade);
contexto.SaveChanges();
}
}
The gambiarra only makes you delete the record to then insert again, which does not correctly solve the problem. Could you please fix my Fork on Github so I can simulate the problem by?
– Leonel Sanches da Silva
@Ciganomorrisonmendez, edited the github, see if now it works!
– Diego Zanardo
@Ciganomorrisonmendez, I added an answer. It would be like you to take a look?
– Diego Zanardo