3

このエラーは私を夢中にさせています。以前は機能していました。今はそうではありません。何が原因なのかわからず、多くのものを失うことなくロールバックすることはできません。

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

更新: 古い作業データベースのスクリーンショット ここに画像の説明を入力 ここに画像の説明を入力

4

3 に答える 3

1

一度に 1 つのファイルを数時間置き換えた後、私の DAL コンテキスト ファイルにはこの流暢な API ステートメントが含まれているようです。

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

追加した理由はわかりませんが、コメントアウトするとエラーがなくなりました。誰かがそれを説明できれば、それは甘いでしょう。

コメントを削除できるようにしたので、今は機能しません。

于 2013-01-29T21:24:57.433 に答える
1

私の場合、xml ソースに 1 対多の関係があることがわかりました。

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

つまり、実際には 1 対多ではなく 1 対 1 でした。私は次のように修正しました:

  <one-to-one name="physicalLocations"
           propertyDefinintion="Defines the physical location of the department"
           typeName="POCO.BO.Location"
           nullable="false"
           dbColumn="DepartmentId" />
于 2015-12-03T19:04:13.933 に答える
1

私はまったく同じエラーを抱えていましたが、最終的には同じ原因でした.EFは関係の性質について一貫性のない理解を与えられていました.

私の場合、DB 構造を個別に管理するアノテーション ベースのモデルを使用しています。うっかりモデルの設定を間違えてしまいました...

DB構造:

  • 親テーブルと子テーブルがありました。一人の親から多くの子供へ。
  • 子テーブルには、Parent に FKing する ParentId 列があります。
  • 子テーブルには人工 PK がありません。
  • 子テーブルには、ParentId + Col1 + Col2 の Natural PK があります。

不適切な 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 は関係が 1 対 1 であると推測し、すべてがうまくいかなかったと思います。

モデル クラスの残りのナチュラル キーをマークすると、エラーが修正されました。

良い 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; }
于 2019-05-09T07:19:20.913 に答える