Good morning, I have the following classes:
CONSUL_CA_Aluno:
public class CONSUL_CA_Aluno
{
public int Id { get; set; }
public string Nome { get; set; }
public int Cpf { get; set; }
public string Email { get; set; }
public string Senha { get; set; }
public int Ativo { get; set; }
public virtual ICollection<CONSUL_CA_Curso> CONSUL_CA_Cursos { get; set; }
}
CONSUL_CA_Course:
public class CONSUL_CA_Curso
{
public int Id { get; set; }
public string Nome { get; set; }
public int Ativo { get; set; }
public string Ministrante { get; set; }
public string Duracao { get; set; }
public int CargaHoraria { get; set; }
public string LocalCurso { get; set; }
public ICollection<CONSUL_CA_Aluno> CONSUL_CA_Alunos { get; set; }
}
In the database I have the table CONSUL_CA_CourseAluno where the class data will be stored.
When testo:
CONSUL_CA_Aluno aluno = new CONSUL_CA_Aluno();
aluno.Ativo = 1;
aluno.Cpf = 1321;
aluno.Email = "email";
aluno.Nome = "diididid";
aluno.Senha = "123";
aluno.CONSUL_CA_Cursos = contexto.Cursos.ToList();
aluno.CONSUL_CA_Cursos = aluno.CONSUL_CA_Cursos.Select(curso => contexto.Curso.FirstOrDefault(x => x.Id == curso.Id)).ToList();
contexto.Aluno.Add(aluno);
contexto.SaveChanges();
Displays the following error:
An unhandled exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll
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.