entity-frameworkasp.net-dynamic-dataentitydatasource

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


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.


Solution

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