Unable to pass object of type 'System.Collections.Generic.List`1 [ModelA]' for input 'ModelA'
This error is driving me crazy. It worked. This is no longer the case. Don't know what changes caused it and I can't roll back without losing a lot.
Unable to cast object of type 'System.Collections.Generic.List`1[Literrater.Models.ranges]'
to type 'Literrater.Models.ranges'.
Here's the model.
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; }
}
Action that causes the error
[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 is published.
{"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"
}
Update: screenshots from the old working database
source to share
After hours of replacing one file at a time, it seems as though my DAL context file had this loose API request.
modelBuilder.Entity<ranges>().HasRequired(p => p.Comment);
I don't know why I added it, but by commenting this out, the error went away. If someone can explain that would be nice.
It turns out I did this to remove a comment that is not working right now.
source to share
In my case, it turned out that I had a one-to-many relationship in my XML source:
<one-to-many name="physicalLocations"
propertyDefinintion="Defines the physical location of the department"
typeName="POCO.BO.Location"
nullable="false"
dbColumn="DepartmentId" />
But there was still the value of the foreign key relationship in the generated C # file:
/// <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; } }
So it was actually one-to-one rather than one-to-many. I fixed it this way:
<one-to-one name="physicalLocations"
propertyDefinintion="Defines the physical location of the department"
typeName="POCO.BO.Location"
nullable="false"
dbColumn="DepartmentId" />
source to share
I had the exact same error and ended up with the same reason - EF was given an inconsistent understanding of the nature of relationships.
In my case, we are using annotation based models with an independently managed DB structure. I accidentally set up the model incorrectly ...
Database structure:
- I had a parent table and a children's table. One parent for many children.
- The child table has a ParentId column FKing to Parent.
- The child table does not have an artificial PC.
- The child table has a natural PK ParentId + Col1 + Col2.
Bad EF Setup:
In the parent
public ICollection<TriangleCell> Cells { get; set; }
The child has
[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; }
I suppose that since only ParentId was tagged as Key
EF assumed the relationship should be one-to-one and things went wrong.
Labeling the rest of the Natural Key in the model class fixed the bug.
Nice EF setup:
In the parent
public ICollection<TriangleCell> Cells { get; set; }
The child has
[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; }
source to share