7
Good morning, I have the following classes:
Consul_ca_student:
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_curso:
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_cursoaluno where the class data will be stored.
When I test:
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 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.
Is there any need to use these encrypted names?
CONSUL_CA_Aluno
could be replaced only byAluno
, nay?– Leonel Sanches da Silva
In the database these nomenclatures are used, so to maintain the standard, I used tbm!
– Diego Zanardo
Not necessary. You can use Attribute [Table("Consul_ca_student")] before class declaration
Aluno
that the Entity Framework points to you.– Leonel Sanches da Silva
I didn’t. Right, but as for the error in insertion, some help?
– Diego Zanardo
I’m writing your answer. Just a moment.
– Leonel Sanches da Silva