0
I’ve just started the study on Asp.Net MVC with Entityframework, I’m having a go at saving the following case:
public class Aluno
{
public int Codigo { get; set; }
public string Nome { get; set; }
public DateTime DataNascimento { get; set; }
public virtual IList<Afericao> RelacaoAfericoes { get; set; }
}
public class Professor
{
public int Codigo { get; set; }
public string Nome { get; set; }
public DateTime DataNascimento { get; set; }
}
public class Afericao
{
public int Codigo { get; set; }
public int ProfessorId { get; set; }
public virtual Professor Professor { get; set; }
public int AlunoId { get; set; }
public virtual Aluno Aluno { get; set; }
public float Peso { get; set; }
public float Altura { get; set; }
}
public class AlunoConfiguration : EntityTypeConfiguration<Aluno>
{
public AlunoConfiguration()
{
ToTable("Alunos");
HasKey(p => p.Codigo);
Property(p => p.Codigo)
.HasColumnName("AlunoId");
Property(p => p.Nome)
.HasColumnName("Nome")
.IsRequired();
Property(p => p.DataNascimento)
.HasColumnName("DataNascimento")
.IsRequired();
}
}
public class ProfessorConfiguration : EntityTypeConfiguration<Professor>
{
public ProfessorConfiguration()
{
ToTable("Professores");
HasKey(p => p.Codigo);
Property(p => p.Codigo)
.HasColumnName("ProfessorId");
Property(p => p.Nome)
.HasColumnName("Nome")
.IsRequired();
Property(p => p.DataNascimento)
.HasColumnName("DataNascimento")
.IsRequired();
}
}
public class AfericaoConfiguration : EntityTypeConfiguration<Afericao>
{
public AfericaoConfiguration()
{
ToTable("Afericoes");
HasKey(af => af.Codigo);
Property(af => af.Codigo)
.HasColumnName("AfericaoId");
Property(af => af.ProfessorId)
.HasColumnName("ProfessorId");
HasRequired(af => af.Professor)
.WithMany()
.HasForeignKey(af => af.ProfessorId);
Property(af => af.AlunoId)
.HasColumnName("AlunoId");
HasRequired(af => af.Aluno)
.WithMany(a => a.RelacaoAfericoes)
.HasForeignKey(af => af.AlunoId);
Property(af => af.Peso)
.HasColumnName("Peso");
Property(af => af.Altura)
.HasColumnName("Altura");
}
}
I Can Include a Student, Teacher and Measurement, after inserting a measurement is giving error. Someone would know answer me the pq?
and another question:
Contexto.Alunos.Include("RelacaoAfericoes")
.Where(x => x.Codigo == pAlunoId).FirstOrDefault();
Why not return the list of Relacaoafericao?
Put the mistake that is giving, without it there is no way to do much.
– Ricardo
Ricardo, the message is this: Validation failed for one or more entities. See 'Entityvalidationerrors' Property for more Details.
– Marcelo Jesualdo
Here’s what I’m trying to do:
– Marcelo Jesualdo
var alunoDomain = _alunoAppService.Getalunoscomafericoes(1); alunoDomain.RelacaoAfericoes.Add(afericaDomain);
– Marcelo Jesualdo
Public student Getalunoscomafericoes(int pAlunoId) { Return Context.Alunos.Include("Relacaoafericoes") . Where(x => x.Code == pAlunoId). Firstordefault(); }
– Marcelo Jesualdo
More does not return to Relacaoafericao, then when will save the error, because I already have a saved record and it is likely that it is generating a record with the same code. That’s what I looked at?
– Marcelo Jesualdo