简体   繁体   中英

'System.Reflection.TargetInvocationException' occurred in EntityFramework.SqlServer.dll

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; }
    }

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM