0

I am using the unit of work with repository pattern in my ASP.NET project together with Entity Framework 6.

The problem I am facing is, is that when I add a new object to the database, its not directly active for Lazy loading when I call that same object again.

For example:

I have a Attendance object which contains 2 virtual links to DayCode and Employee.

public partial class Attendance
{
    public Attendance()
    {
        this.Id = -1;
        this.EmployeeId = -1;
        this.Present = false;
        this.Time = 0;
        this.Closed = false;
        this.Correction = false;
    }

    public int Id { get; set; }
    public int EmployeeId { get; set; }
    public bool Present { get; set; }
    public System.DateTime Date { get; set; }
    public string Remark { get; set; }
    public int Time { get; set; }
    public Nullable<short> DayCodeId { get; set; }
    public bool Closed { get; set; }
    public bool Correction { get; set; }

    public virtual Employee Employee { get; set; }
    public virtual DayCode DayCode { get; set; }
}

My DayCode class:

public partial class DayCode
{
    public DayCode()
    {
        this.Id = -1;
        this.Code = "";
        this.Attendances = new HashSet<Attendance>();
    }

    public short Id { get; set; }
    public string Code { get; set; }
    public string Title { get; set; }
    public string Color { get; set; }

    public virtual ICollection<Attendance> Attendances { get; set; }
}

My click event of a button on my webpage:

    protected void btnSaveCorrection_Click(object sender, EventArgs e)
    {
        Attendance attendance = GetItem();

        try
        {
            unitOfWork.AttendanceRepository.Add(attendance);
            unitOfWork.Save();
        }
        catch
        {
            ShowMessage("Problemen ondervonden, probeer opnieuw!", "alert alert-danger");
        }

        // Load attendances
        LoadAttendances(3); // Test Michael
        LoadCorrections(3); // Test Michael
    }

My LoadCorrections method:

    private void LoadCorrections(int employeeId) // Test _EmployeeId
    {
        //...

        Employee _employee = unitOfWork.EmployeeRepository.LoadItem(employeeId);

        int counter = 0;
        foreach (Attendance attendance in _employee.Attendances.Where(
                e => e.Correction && e.Date.Month == _Date.Month
            ).OrderBy(e => e.Date).ThenBy(e => e.Id))
        {
            _bodyRow = new BTableBodyRow();
            _bodyRow._CssClass = "";

            //...

            _cellBody = new BTableBodyCell();
            _cellBody._Content = attendance.DayCode.Code + " ( " + attendance.DayCode.Title + " )";

            //Here I get a nullreference exception!!!!!!!!!!!!!!!!!!

            _cellBody._CssClass = "left";
            _bodyRow._BodyCells.Add(_cellBody);

            //...

            _bodyRows.Add(_bodyRow);

            counter++;
        }

        //...
    }

Do I have to update in some of way the Lazy loading or... ?

1 Answer 1

0

Found the solution reading the answer of Entity Framework Code First not lazy loading after save

The problem was that I only created a new instance of the POCO class and not the proxy. In my Repository pattern I created a new virtual method Create which creates this proxy.

public virtual T Create()
{
    return this._dbSet.Create<T>();
}

And instead of creating a new instance of the attendance class using:

Attendance attendance = new Attendance();

I use the Repository pattern method Create:

Attendance attendance = unitOfWork.AttendanceRepository.Create();

Now the null reference exception is gone.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.