Query using the Entity Framework

Asked

Viewed 132 times

1

I’m a beginner in the Entity Framework and I picked up a software for maintenance that uses the Entity.

I need to create a report and do the following SQL search using Entity:

select p.RA, p.Nome, p.Modulo, a.Descricao  from inscricao as i, Participante as p, Atividade as a
where i.ParticipanteId = p.ParticipanteId and 
       i.AtividadeId = a.AtividadeId
order by p.Modulo

And in Controller I use the following "tried" method to do the above search using Entity.

public ActionResult ParticipantesInscritosAtividade(RelatorioParticipantesInscritosAtividadeVM ViewModel) {
    if (ModelState.IsValid)
    {
        var participantes = db.Inscricoes
                    .Join(db.Participantes, ins => ins.ParticipanteId, pa => pa.ParticipanteId, (ins, pa) => new { ins, pa })
                    .Join(db.Atividades, ins1 => ins1.ins.AtividadeId, atv => atv.AtividadeId, (ins1, atv) => new { ins1, atv })
                    .Select(query => new
                    {
                        ParticipanteId = query.ins1.pa.ParticipanteId,
                        RA = query.ins1.pa.RA,
                        Aluno = query.ins1.pa.Nome,
                        AtividadeId = query.atv.AtividadeId,
                        Atividade = query.atv.Titulo,
                        Modulo = query.ins1.pa.Modulo
                    }).ToList();

       if(participantes.Count() == 0) {
            ViewModel.Mensagens.Add(new MensagemVM { Mensagem = MessageUtils.MESSAGE_NO_REPORT_DATA, TipoMensagem = MensagemVM.TipoMensagemEnum.Warning });
        }
        else
        {
            foreach (var p in participantes)
            {
                 ViewModel.Participantes.Add(new RelParticipantesInscritosAtividadeVM { RA = p.ins1.pa.RA.Value, Nome =  p.ins1.pa.Nome , Modulo = p.ins1.pa.Modulo.v });
            }
        }
    }

    PrepararRelatorioParticipantesInscritosAtividadeViewModel(ViewModel);
    return View(ViewModel);
}

When I do this the Modelstate.isValid returns that the object cannot receive a null value.

My model is like this:

public class RelParticipantesInscritosAtividadeVM
{
    [Display(Name = "R.A.")]
    public long RA { get; set; }

    [Display(Name = "Nome")]
    public string Nome { get; set; }

    [Display(Name = "Modulo")]
    public int Modulo { get; set; }
}

No required field and yet Model.isValid keeps giving the error.

My SQL is just an example of what I need to do, using Join I performed the query but I did not return the correct values, I ask for help translating my SQL to the Entity,

public class RelatorioParticipantesInscritosAtividadeVM
{
    public RelatorioParticipantesInscritosAtividadeVM()
    {
        this.Participantes = new List<RelParticipantesInscritosAtividadeVM>();
        this.Mensagens = new List<MensagemVM>();

    }

    [DataType(DataType.DateTime)]
    [Display(Name = "Data")]
//    [Required(ErrorMessage = "Campo obrigatório")]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy HH:mm}", ApplyFormatInEditMode = true)]
    [UIHint("DataGeral")]
    public DateTime Data { get; set; }

    [Display(Name = "Período")]
  //  [Required(ErrorMessage = "Campo obrigatório")]
    public Rel2PeriodoEnumVM Periodo { get; set; }
    public SelectList PeriodosList { get; set; }

    [Display(Name = "Curso")]
   // [Required(ErrorMessage = "Campo obrigatório")]
    public string CursoId { get; set; }
    public SelectList CursosList { get; set; }

   [Display(Name = "Atividade")]
  //  [Required(ErrorMessage = "Campo obrigatório")]
    public String AtividadeId { get; set; }
    public SelectList AtividadeList { get; set; }

    [Display(Name = "Termo")]
  //  [Required(ErrorMessage = "Campo obrigatório")]
    public short Termo { get; set; }
    public SelectList TermosList { get; set; }



    public IList<RelParticipantesInscritosAtividadeVM> Participantes { get; set; }
    public IList<MensagemVM> Mensagens { get; set; }
  //  public IList<RelAtividadeVM> Atividade { get; set; }

}

}

As suggested, I created a breakpoint on the requested line but it is stated that the variable does not exist in the current context. When going to the next line (F11), gives this infamous error:

The null object must have a value.

Description: An untreated exception occurred during the execution of current web request. Examine stack tracking to get more information about the error and where it originated in the code.

Exception Details: System.Invalidoperationexception: The null object must have a value.

Error of Origin:

Line 113: { line 114: Line 115: var errors = Modelstate.Values.Selectmany(v => v.Errors); Line 116: if (Modelstate.Isvalid) Line 117: {

  • 1

    The error is because some property of your model is mandatory (Required) and is receiving null.

  • Your SQL example is not even making a Join. As the friend said, Modelstate.Isvalid has nothing to do with the query, it doesn’t even come close to making the query.

  • The model you get in the method and it says it’s invalid is the Reportsrepresentate activitiesdev (not the one you put above)

  • Anyway, do you know which field he criticizes? breakpoint in the .IsValid, the mouse cursor on ModelState and goes on Values.. there you have all the properties and within each one the indication whether you have an error or not. Picks up which index is wrong and see in Keys which one is in trouble. I hope it helps.

  • I did as you asked: I have 5 Keys and 5 values and no error is shown. My Keys are Date, Periodo, Cursoid, Activity

  • @Rafaelchristófano, put the error message then, to see if there is something that passed beaten

  • 1

    Use that code: var errors = ModelState.Values.SelectMany(v => v.Errors); and see what mistakes are happening

  • Did you ever check that the "Viewmodel" object is not null? It may be that your problem is in the request..

  • Viewmodel is passing all the information set in the view.

Show 4 more comments
No answers

Browser other questions tagged

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