Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An exception occurred in EntityFramework

I am doing my MVC application, I have already done a part of it (using Entity Framework) and works fine. However, no I got an error:

+       InnerException  {"The class 'ClassDeclarationsThsesis.Models.Subject' has no parameterless constructor."}   System.Exception {System.InvalidOperationException}


An exception of type 'System.Reflection.TargetInvocationException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

My class looks like this:

public partial class Subject
{
    private int v;
    private int userid;

    public Subject(int v, int userid, string name)
    {
        this.class_id = v;
        this.user_id = userid;
        this.name = name;
    }


    public int class_id { get; set; }
    public int user_id { get; set; }
    public string name { get; set; }

    public virtual Group Group { get; set; }
    public virtual Subjects_Users Subjects_Users { get; set; }
    public virtual Task Task { get; set; }
}

How do I solve this issue? I think that i have already tried everything that I could and I found here, but nothing worked.
Also, I already read data from database and use it and it works. It only failed in this case (at least so far). I am really confused about this.

like image 247
Maciej Miśkiewicz Avatar asked Apr 15 '26 01:04

Maciej Miśkiewicz


1 Answers

You need to add parameterless constructor to Subject class:

public partial class Subject
{
    private int v;
    private int userid;

    public Subject()
    {
    }

    public Subject(int v, int userid, string name)
    {
        this.class_id = v;
        this.user_id = userid;
        this.name = name;
    }


    public int class_id { get; set; }
    public int user_id { get; set; }
    public string name { get; set; }

    public virtual Group Group { get; set; }
    public virtual Subjects_Users Subjects_Users { get; set; }
    public virtual Task Task { get; set; }
}
like image 131
Damian Avatar answered Apr 17 '26 14:04

Damian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!