-2

I'm calling this method by CascadingDropDownListFor and I'm getting an exception:

An exception of type 'System.Reflection.TargetInvocationException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

public JsonResult GetRaca(string especieId)
{
    int esp = Convert.ToInt32(especieId);
    var rac = db.Raca.Where(c => c.EspecieId == esp).ToList();
    var racas = new List<SelectListItem>();
    foreach (var ra in rac)
    {
        var racaConteudo = db.RacaConteudo
            .Where(c => c.RacaId == ra.RacaId)
            .Where(c => c.IdiomaId == 1)
            .First(); // <= The exception occurred here

        racas.Add(new SelectListItem 
        {
            Text = racaConteudo.RacaId.ToString(), 
            Value = racaConteudo.NomePopular 
        });
    }

    return Json(racas, JsonRequestBehavior.AllowGet);
}

The entity:

[Table("RacasConteudo")]
    public class RacaConteudo
    {
        public RacaConteudo(long RacaId, string NomeCientifico, string NomePopular, long IdiomaId)
        {
            this.RacaId = RacaId;
            this.NomeCientifico = NomeCientifico;
            this.NomePopular = NomePopular;
            this.IdiomaId = IdiomaId;
        }

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public long RacaConteudoId { get; set; }

        [ForeignKey("RacaId")]
        public virtual Raca Raca { get; set; }
        public long RacaId { get; set; }

        [DataType(DataType.Text)]
        [Display(Name = "Nome Cientifico")]
        public string NomeCientifico { get; set; }

        [DataType(DataType.Text)]
        [Display(Name = "Nome Popular")]
        public string NomePopular { get; set; }

        [ForeignKey("IdiomaId")]
        [Display(Name = "Idioma")]
        public virtual Idioma Idioma { get; set; }
        public long IdiomaId { get; set; }
    }
4
  • 1
    Can you show your entity objects? Do they all have parameterless constructors for example? Commented Feb 2, 2017 at 13:57
  • What is the type of the db.RacaContuendo property? Commented Feb 2, 2017 at 13:57
  • 2
    What's the inner exception text? Commented Feb 2, 2017 at 13:57
  • Possible duplicate of stackoverflow.com/questions/22666607/… Commented Feb 2, 2017 at 18:29

1 Answer 1

-1

Remove parameterized constructor public RacaConteudo(long RacaId, ..., long IdiomaId), and make the class partial.

[Table("RacasConteudo")]
public partial class RacaConteudo
        ^^^^^
{
   /* public RacaConteudo(...)  {} */

   [Key]
   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
   public long RacaConteudoId { get; set; }
   ....
}

Based on your updated question, Raca and RacaConteudo have relationship.

If so, you could even retrieve the desired result in single query which is a lot faster than querying multiple RacaConteudos for each and every Raca.

public JsonResult GetRaca(string especieId)
{
    int esp = Convert.ToInt32(especieId);

    var result = (from c in db.RacaConteudo
        where c.Raca.EspecieId == esp && c.IdiomaId == 1
        select new {Text = c.NomePopular, Value = c.RacaId.ToString()}).ToList();    

    return Json(result, JsonRequestBehavior.AllowGet);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I have no problem with suggesting better ways to do things, but when I wrote that, your answer had nothing about the parameterless constructors which does answer the question. However, now the question is a dupe and should have been closed as such.
@DavidG No, I did not. There is nothing wrong in your comment; besides, I appreciate your feedback. Down-votes in question and answers might have triggered the moderator attention.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.