LINQ error: “<object> has no parameterless constructor.”

后端 未结 2 617
一生所求
一生所求 2021-01-22 01:31

I have a class method GetMS() as defined below, but when the method is called I get an error stating \'SLRD\' has no parameterless constructor. I don\'

相关标签:
2条回答
  • 2021-01-22 01:54

    I don't understand why a parameterless constructor is even being called.

    My guess is that you are populating ChildRequests lazily via a Linq/EF query, so the objects are not created until you iterate over them (which is done as part of the First method). At that point, EF tries to create an instance using a parameterless constructor which does not exist.

    0 讨论(0)
  • 2021-01-22 02:16

    The error you get is pretty clear. You missed to define a parameterless constructor.

    public class SLR : BaseEntity
    {
        public SLR() : base
        {
        }
    
        // ...
        public virtual ICollection<SLRD> ChildRequests { get; set; }
    }
    

    The reason this is happens is the fact that your base class may have a parameterless constructor. So you have to define one for the derived class, SLR.

    0 讨论(0)
提交回复
热议问题