简体   繁体   中英

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

I am creating a MVC application using Entity Framework. I am trying to access data from my database in a controller this way:

public ActionResult Classes()
{
    ClassDeclarationsDBEntities1 entities = new ClassDeclarationsDBEntities1();
    var model = new ClassesViewModel();
    model.task = entities.Tasks.ToList();
    model.subject = entities.Subjects.ToList();
    model.user = entities.Users.ToList();
    model.group = entities.Groups.ToList();
    return View(model);
}

However, in line with group I get an exception saying:

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

My Model:

   public class ClassesViewModel
    {
        public List<Task> task { set; get; }
        public List<Subject> subject { set; get; }
        public List<Group> group { set; get; }
        public List<User> user { set; get; }
    }

And the Group class:

namespace ClassDeclarationsThsesis.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Group
    {
        public Group(int id, int i, string modelGroupName, int teacherUserId)
        {
            this.class_id = id;
            this.group_id = i;
            this.name = modelGroupName;
            this.user_id = teacherUserId;
        }

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

        public virtual Subject Subject { get; set; }
        public virtual User User { get; set; }
    }
}

I am sure that the table in DB is not empty and contains data. When debugging, it says that it is null. What do I do wrong?

Your Model class has parameterized constructor that's why you are getting this error. Remove parameterized constructor (it would be better if you remove your constructor). Or make one more constructor with no parameters

private Group() {}

Actually you really don't need to pass the values through constructor to initialize your field or properties while using Entity Framework because you are getting records from database or set these values through viewModel or from views.

And constructor only contains reference or collection of another models for navigation property, this helps to instantiate the navigation collections to guard you a little bit against NullReferenceExceptions . But it is not required.So it would better remove your constructor from every class where you have used your constructor like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question 'System.Reflection.TargetInvocationException' occurred in EntityFramework.SqlServer.dll An exception of type 'System.Reflection.TargetInvocationException' occurred in System.ni.dll but was not handled in user code An exception of type 'System.InvalidOperationException' occurred in EntityFramework.SqlServer.dll but was not handled in user code An exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.ni.dll but was not handled in user code: in Xamarin Form An exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll but was not handled in user code MVC error- An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.SqlServer.dll but was not handled in user code Raw Sql - System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.SqlServer.dll but was not handled in user code An unhandled exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.SqlServer.dll An exception of type 'System.Reflection.TargetInvocationException' occurred in System.Windows.ni.dll windows phone 8 When it occurs An unhandled exception of type “'System.Reflection.TargetInvocationException' occurred in System.Windows.ni.dll” inWindows Phone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM