Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法强制转换类型为';System.Collections.Generic.List`1[ModelA]';输入';A型和x27型;_C#_Json_Data Binding_Asp.net Mvc 4_Entity Framework 5 - Fatal编程技术网

C# 无法强制转换类型为';System.Collections.Generic.List`1[ModelA]';输入';A型和x27型;

C# 无法强制转换类型为';System.Collections.Generic.List`1[ModelA]';输入';A型和x27型;,c#,json,data-binding,asp.net-mvc-4,entity-framework-5,C#,Json,Data Binding,Asp.net Mvc 4,Entity Framework 5,这个错误快把我逼疯了。它过去是有用的。现在没有了。我不知道是什么变化导致了它,我不能不损失很多东西就回滚 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 {

这个错误快把我逼疯了。它过去是有用的。现在没有了。我不知道是什么变化导致了它,我不能不损失很多东西就回滚

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


}
正在发布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"
 }
更新:来自旧工作数据库的屏幕截图

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

 modelBuilder.Entity<ranges>().HasRequired(p => p.Comment);
modelBuilder.Entity().HasRequired(p=>p.Comment);
我不知道为什么我添加了它,但是注释它使错误消失了。如果有人能解释,那就太好了


事实证明,我这样做是为了删除注释,但现在不起作用。

在我的例子中,结果证明我的xml源中存在一对多关系:

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

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

//
///定义部门的物理位置
/// 
public const字符串F_PHYSICALLOCATIONS=“PHYSICALLOCATIONS”;
[外键(“物理位置”)]
公共字符串DepartmentId{get;set;}
私人位置(物理位置);;
公共虚拟位置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" />

我犯了完全相同的错误,最终也是相同的原因——EF对这种关系的性质理解不一致

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

数据库结构:
  • 我有一个父表和一个子表。一个父母有很多孩子
  • 子表的ParentId列与父表绑定
  • 子表没有人工PK
  • 子表具有ParentId+Col1+Col2的自然主键
错误的EF设置: 父母

    public ICollection<TriangleCell> Cells { get; set; }
    public ICollection<TriangleCell> Cells { get; set; }
我认为,因为只有ParentId被标记为
,因此EF推断出关系必须是一对一的,并且一切都出错了

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

良好的EF设置: 父母

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

如果错误在
db.Comments.Add(comment)
中,我们确实需要知道数据库是什么。。。听起来好像你在某个地方遇到了模型不匹配的问题。@Jon我正在使用实体框架进行代码优先迁移(现在都是离线本地开发),如果这是你的意思的话?对-我们以前不知道它是EF。您的
列表如何显示在数据库中?为什么它不像其他的一样是一个虚拟的
ICollection
属性?我没有理由?它以前就起作用了。我只是在学习这一切。我将其更改为ICollection删除/种子数据库(无效)。那么该列在数据库中是什么样子的呢?其他多元素属性有效吗?您确实需要将JSON端与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; }