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; }
}
db.RacaContuendoproperty?