【问题标题】:'System.Reflection.TargetInvocationException' occurred in EntityFramework.SqlServer.dllEntityFramework.SqlServer.dll 中发生“System.Reflection.TargetInvocationException”
【发布时间】:2017-02-02 13:54:11
【问题描述】:

我正在通过CascadingDropDownListFor 调用此方法,但出现异常:

EntityFramework.SqlServer.dll 中出现“System.Reflection.TargetInvocationException”类型的异常,但未在用户代码中处理

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

实体:

[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.RacaContuendo 属性的类型是什么?
  • 内部异常文本是什么?

标签: c# .net sql-server entity-framework visual-studio


【解决方案1】:

移除参数化构造函数 public RacaConteudo(long RacaId, ..., long IdiomaId),并使类部分

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

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

根据您更新的问题,RacaRacaConteudo 有关系。

如果是这样,您甚至可以在单个查询中检索所需的结果这比为每个 Raca 查询多个 RacaConteudo 快得多

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

【讨论】:

  • 我对提出更好的做事方法没有意见,但是当我写下这些时,您的回答与 确实 回答问题的无参数构造函数无关。但是,现在问题是dupe,应该这样关闭。
  • @DavidG 不,我没有。你的评论没有错;此外,感谢您的反馈。对问题和答案的投反对票可能会引起主持人的注意。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-21
相关资源
最近更新 更多