Null Reference to Entity Association when overriding ToString in partial class - EntityDataSource.Include is being ignored

StackOverflow https://stackoverflow.com/questions/15886884

문제

I am using Dynamic Data for Entity Framework, unfortunately I'm stuck on .NET 3.5, so it's EF1, and at the moment this cannot change.

So my problem is this, I have tried adding the EntityDataSource.Include property in a couple of ways to deal with the null reference I am getting when overriding the ToString method in a partial class for table1. I have tried setting the Include in markup on the EntityDataSource declaration and also by setting EntityDataSource.Include = "table2.table3" in the EntityDataSource.Selecting event, both no luck.

As you can see, I need to add an include to an association of an association. I want to display "table3.name + table2.Date" in the override ToString method for table2, and reflected in the dropdownlist for the association reference when in edit mode on table1.

Mind you, the Include works just fine on the GridDataSource, for whatever reason I am having issues on the DetailsDataSource.

도움이 되었습니까?

해결책

I have found a work-around, that being to check if the association reference is loaded in the ToString override, and if not load it.

public override string ToString()
{
    if (this.Course == null)
    {
        if (!this.CourseReference.IsLoaded)
            this.CourseReference.Load();

        return this.Course.Name + " - " + string.Format("{0:yyyy-MM-dd}", this.StartDate.Date);
    }
    else
        return this.Course.Name + " - " + string.Format("{0:yyyy-MM-dd}", this.StartDate.Date);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top