延迟加载对新添加的对象不起作用

时间:2014-12-06 20:17:24

标签: entity-framework lazy-loading entity-framework-6

我在ASP.NET项目中使用unit of workrepository pattern以及Entity Framework 6。

我面临的问题是,当我向数据库添加一个新对象时,当我再次调用同一个对象时,它不会直接激活Lazy loading

例如:

我有一个Attendance object,其中包含2个指向DayCodeEmployee的虚拟链接。

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

我的DayCode课程:

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

我的网页上的按钮的点击事件:

    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
    }

我的LoadCorrections方法:

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

        //...
    }

我是否必须以某种方式更新Lazy loading或......?

1 个答案:

答案 0 :(得分:0)

找到解决Entity Framework Code First not lazy loading after save

答案的解决方案

问题是我只创建了POCO类的新实例而不是代理。 在我的Repository模式中,我创建了一个新的虚拟方法Create,用于创建此代理。

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

而不是使用以下方法创建考勤类的新实例:

Attendance attendance = new Attendance();

我使用Repository模式方法Create

Attendance attendance = unitOfWork.AttendanceRepository.Create();

现在空引用异常消失了。