Error System.NullReferenceException while accessing Parent entity property in child entity


I am trying to access a property of parent entity which is on one side of relation while in child entity but it is giving me null reference exception. May be its problem of lazy loading which is not available in entity framework core 2.0. How to access it in core 2.0. These are details of problem

 public partial class TblEmployee
    {
        public TblEmployee()
        {
            AttendanceTable = new HashSet<AttendanceTable>();
        }

        public int EmpId { get; set; }
        public string EmployeeName { get; set; }

        public virtual ICollection<AttendanceTable> AttendanceTable { get; set; }
    }
 public partial class AttendanceTable
    {
        public int Id { get; set; }
        public int AttendanceId { get; set; }
        public int EmployeeId { get; set; }
        public string ScanType { get; set; }
        public DateTime DateAndTime { get; set; }

        public virtual TblEmployee Employee { get; set; }
    }
 public class EmployeeAtt
    {
        public int Id { get; set; }
        public string Emp_name { get; set; }
        public DateTime? Date { get; set; }
        public TimeSpan InTime { get; set; }
        public TimeSpan OutTime { get; set; }

        //New Field
        public string InoutTime { get; set; }
    }
public class ReportsController : Controller
    {
        private readonly CheckItContext _context;

        public ReportsController(CheckItContext context)
        {
            _context = context;
        }

        // GET: Reports
       
        public ActionResult Index() 
        {
            int Month = DateTime.Now.Month;

            List<EmployeeAtt> empWithDate = new List<EmployeeAtt>();
            using (_context)
            {
                var List = _context.AttendanceTable.Where(a => a.DateAndTime.Month == Month).OrderBy(a => a.DateAndTime).ToList();

                var empatt = new EmployeeAtt();
                foreach (var emp in List)
                {
                    //create new employee object fo each loop

                    //Get Array list for each emp using it's Id and sort list according to datetime
                    var attendnaceList = List.Where(a => a.EmployeeId == emp.EmployeeId).OrderBy(a => a.DateAndTime).ToArray();

                    //Get Count
                    var Count = attendnaceList.Count();

                    //Loop through all values and increment it by 2 as we will compare two values in/out
                    for (var i = 0; i < Count; i = i + 2)
                    {
                        empatt = new EmployeeAtt();
                        //Although no need to check but confirm if both the values are of same date,which we will compare
                        if (attendnaceList[i].DateAndTime.Date == attendnaceList[i + 1].DateAndTime.Date)
                        {
                            //Check which value is less using Time now,as it is sorted  first value will be less than second
                            if (attendnaceList[i].DateAndTime.TimeOfDay < attendnaceList[i + 1].DateAndTime.TimeOfDay)
                            {
                                empatt.Id = Convert.ToInt32(emp.EmployeeId);
                                empatt.Emp_name = emp.Employee.EmployeeName;
                                empatt.Date = attendnaceList[i].DateAndTime.Date;

                                //inout time
                                empatt.InoutTime = attendnaceList[i].DateAndTime + "-" + attendnaceList[i + 1].DateAndTime;

                                //to avoid duplicate check if value already exists or not, if not add one
                                if (empWithDate.Where(a => a.Emp_name == attendnaceList[i].Employee.EmployeeName && a.Date.Value.Date == attendnaceList[i].DateAndTime.Date).Count() == 0)
                                {
                                    empWithDate.Add(empatt);
                                }

                            }
                        }
                    }
                }
                empWithDate.OrderBy(a => a.Date).ToList();

            }

Second line of code is throwing error

empatt.Id = Convert.ToInt32(emp.EmployeeId);
empatt.Emp_name = emp.Employee.EmployeeName;
empatt.Date = attendnaceList[i].DateAndTime.Date;

Asked by:- MuhammadFaisal81
0
: 148 At:- 1/19/2019 9:01:26 AM
ASP.NET NullReferenceException Entity Framework Core 2.0

Provide screenshot and more error details with proper explanation of your code and desired output. 0
By : vikas_jk - at :- 1/20/2019 7:41:35 AM







Login/Register to answer
Or
Post answer directly by entering your email-id & Name

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use