locked
Null Association when overriding ToString in partial class - EntityDataSource.Include is being ignored RRS feed

  • Question

  • User503670473 posted

    I am using DynamicData 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.

    Any help is appreciated.

    Friday, April 5, 2013 1:08 PM

Answers

  • User503670473 posted

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



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 9, 2013 2:20 PM

All replies

  • User503670473 posted

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



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 9, 2013 2:20 PM
  • User260886948 posted

    Hi,

    I am very glad that you have solved your problem by yourself. It will help the others who meet the same issue as you.

    If you have any other problem, welcome to post it in the asp.net forums.

    Best Regards,
    Amy Peng 

    Friday, April 12, 2013 5:34 AM