【问题标题】:Unable to cast object of type 'System.Collections.Generic.List`1[ModelA]' to type 'ModelA'无法将“System.Collections.Generic.List`1[ModelA]”类型的对象转换为“ModelA”类型
【发布时间】:2013-01-29 19:30:06
【问题描述】:

这个错误快把我逼疯了。它曾经工作过。现在没有了。不知道是什么变化导致了它,我不能在不丢失很多东西的情况下回滚。

 Unable to cast object of type 'System.Collections.Generic.List`1[Literrater.Models.ranges]' 
 to type 'Literrater.Models.ranges'.

这是模型。

public class Comment
{
    [Key]
    public int CommentID { get; set; }
    public int ProjectDocID { get; set; }
    public int UserID { get; set; }
    public string text { get; set; }
    public string quote { get; set; }
    public DateTime DateCreated { get; set; }

    public virtual ICollection<CommentVote> CommentVote { get; set; }
    public virtual ICollection<CommentReply> CommentReply { get; set; }
    public virtual ICollection<CommentReport> CommentReport { get; set; }
    public ProjectDoc ProjectDoc { get; set; }
    public virtual User User { get; set; }
    public ICollection<ranges> ranges { get; set; }
}
public class ranges
{
    public int ID { get; set; }
    public string start { get; set; }
    public string end { get; set; }
    public string startOffset { get; set; }
    public string endOffset { get; set; }


}

引发错误的操作

    [HttpPost, ActionName("Create")]
    public ActionResult Create(Comment comment)
    {
        comment.DateCreated = DateTime.Now;
        comment.UserID = WebSecurity.CurrentUserId;
        db.Comments.Add(comment); //Error here
        db.SaveChanges();
        return Json(comment);

Json 正在发布。

 {"text":"",
  "ranges":
       [{
            "start":"/p[1]",
            "startOffset":160,
            "end":"/p[1]",
            "endOffset":234
       }],
   "quote":"es. Hadn't they ever heard of potpourri? Each incessant beep, made by the",
   "UserID":"6",
   "ProjectDocID":"1"
 }

更新:旧工作数据库的屏幕截图

【问题讨论】:

  • 如果错误出现在 db.Comments.Add(comment) 中,我们真的需要知道数据库是什么......听起来你在某个地方遇到了模型不匹配。
  • @Jon 我正在使用带有代码优先迁移的实体框架(现在这只是离线本地开发)如果你是这个意思?
  • 对 - 我们之前不知道它是 EF。您的List&lt;ranges&gt; 是如何出现在数据库中的?为什么它不像其他的那样是虚拟的ICollection&lt;ranges&gt; 属性?
  • 我没有理由?它以前工作过。只是学习这一切。我将其更改为 ICollection 删除/植入数据库(无效)。
  • 那么列在数据库中是什么样子的呢?您的其他多元素属性是否有效?您确实需要将 JSON 端与 EF 端分开:让它们分别工作,然后将它们放在一起。

标签: c# json data-binding asp.net-mvc-4 entity-framework-5


【解决方案1】:

在一次替换一个文件几个小时后,我的 DAL 上下文文件似乎有这个流畅的 API 语句。

 modelBuilder.Entity<ranges>().HasRequired(p => p.Comment);

我不知道为什么要添加它,但是将其注释掉会使错误消失。如果有人能解释一下,那就太好了。

原来我这样做是为了删除评论,但现在不起作用。

【讨论】:

    【解决方案2】:

    在我的例子中,我的 xml 源代码中存在一对多关系:

      <one-to-many name="physicalLocations"
               propertyDefinintion="Defines the physical location of the department"
               typeName="POCO.BO.Location"
               nullable="false"
               dbColumn="DepartmentId" />
    

    但是在生成的C#文件中还是有外键关系值:

        /// <summary>
        /// Defines the physical location of the department
        /// </summary>
        public const string F_PHYSICALLOCATIONS = "PhysicalLocations";
        [ForeignKey("PhysicalLocations")]
        public string DepartmentId { get; set; }
        private Location _physicalLocations;
        public virtual Location PhysicalLocations { get { return _physicalLocations; } set { _physicalLocations = value; } }
    

    所以它实际上是一对一而不是一对多。我是这样修复的:

      <one-to-one name="physicalLocations"
               propertyDefinintion="Defines the physical location of the department"
               typeName="POCO.BO.Location"
               nullable="false"
               dbColumn="DepartmentId" />
    

    【讨论】:

      【解决方案3】:

      我遇到了完全相同的错误,最终导致相同的原因 - EF 对关系性质的理解不一致。

      在我的例子中,我们使用基于注释的模型,数据库结构独立管理。我不小心把模型设置错了……

      数据库结构:

      • 我有一个父表和一个子表。一位家长多子女。
      • 子表有 ParentId 列 FKing to Parent。
      • 子表没有人工PK。
      • 子表的自然 PK 为 ParentId + Col1 + Col2。

      错误的 EF 设置:

      在父母中

          public ICollection<TriangleCell> Cells { get; set; }
      

      在孩子中

          [Key]
          public int ParentId { get; set; }
          [ForeignKey(nameof(ParentId ))]
          public ParentTable Parent{ get; set; }
      
          public int Col1 { get; set; }
          public int Col2 { get; set; }
          public int ValueCol { get; set; }
      

      我相信因为只有 ParentId 被标记为Key,所以 EF 推断关系一定是一对一的,一切都出错了。

      在模型类中标记自然键的其余部分修复了错误。

      良好的 EF 设置:

      在父母中

          public ICollection<TriangleCell> Cells { get; set; }
      

      在孩子中

          [Key, Column(Order = 0)]
          public int ParentId { get; set; }
          [ForeignKey(nameof(ParentId ))]
          public ParentTable Parent{ get; set; }
      
          [Key, Column(Order = 1)]
          public int Col1 { get; set; }
          [Key, Column(Order = 2)]
          public int Col2 { get; set; }
      
          public int ValueCol { get; set; }
      

      【讨论】:

        猜你喜欢
        • 2015-09-14
        • 1970-01-01
        • 1970-01-01
        • 2014-03-14
        • 2012-05-22
        • 1970-01-01
        • 2022-01-14
        • 2016-02-10
        • 2019-02-16
        相关资源
        最近更新 更多