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; }
}
Interaction layer 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 registry 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 it 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 can not 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 IntelliTrace:
Exception: Caught: "Violation of PRIMARY KEY constraint 'PK_dbo.SBE_ST_CourseSBE_ST_DoctorCode'. Can not insert duplicate key in object 'dbo.SBE_ST_CourseSBE_ST_Doctor'. 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_CourseSBE_ST_CopyCenter'. Can not insert duplicate key in object 'dbo.SBE_ST_CourseSBE_ST_Doctor_body'. The duplicate key value is (8, 1). The statement has been terminated. " Time: 11/08/2014 15:28:19 Thread: Worker Thread [3508]
I did a GAMBIARRA to solve this problem. (ALMOST GAMBIARRA)
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();
}
}