Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.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# EntityFramework中发生异常_C#_Asp.net_.net_Entity Framework - Fatal编程技术网

C# EntityFramework中发生异常

C# EntityFramework中发生异常,c#,asp.net,.net,entity-framework,C#,Asp.net,.net,Entity Framework,我正在做我的MVC应用程序,我已经做了它的一部分(使用实体框架),并且运行良好。但是,没有,我有一个错误: + InnerException {"The class 'ClassDeclarationsThsesis.Models.Subject' has no parameterless constructor."} System.Exception {System.InvalidOperationException} An exception of type 'Syst

我正在做我的MVC应用程序,我已经做了它的一部分(使用实体框架),并且运行良好。但是,没有,我有一个错误:

+       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
我的班级是这样的:

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; }
}
我如何解决这个问题?我想我已经尝试了我能做的一切,我在这里找到了,但没有任何效果。
另外,我已经从数据库中读取数据并使用它,它也可以工作。它只是在这种情况下失败了(至少到目前为止)。我真的对此感到困惑。

在像C#(或Java)这样的语言中,每个公共类都有自己的无参数构造函数。也就是说,在定义构造函数(任何构造函数)之前,您负责添加所有需要的构造函数

因此,在您的情况下,您需要添加:

public Subject() {}

您需要向Subject类添加无参数构造函数:

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

能否显示导致此错误的linq查询?您可能会重写它以与当前类一起使用。