Mapping Error and Fluent Api

Asked

Viewed 49 times

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, the message is this: Validation failed for one or more entities. See 'Entityvalidationerrors' Property for more Details.

  • Here’s what I’m trying to do:

  • var alunoDomain = _alunoAppService.Getalunoscomafericoes(1); alunoDomain.RelacaoAfericoes.Add(afericaDomain);

  • Public student Getalunoscomafericoes(int pAlunoId) { Return Context.Alunos.Include("Relacaoafericoes") . Where(x => x.Code == pAlunoId). Firstordefault(); }

  • 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?

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.