当在部分类- EntityDataSource中重写ToString时,对实体关联的Null引用.Include被忽略了



我正在使用实体框架的动态数据,不幸的是我被困在。net 3.5上,所以它是EF1,目前这不能改变。

所以我的问题是这样的,我已经尝试添加EntityDataSource。以几种方式包含属性,以处理我在表1的部分类中重写ToString方法时获得的空引用。我试过在EntityDataSource声明上设置Include标记,也试过在EntityDataSource.Selecting事件中设置EntityDataSource.Include = "table2.table3",都没有运气。

如您所见,我需要向关联的关联添加一个include。我想显示table3。name + table2。Date"在表2的override ToString方法中,并且在表1的编辑模式下,在关联引用的下拉列表中反映。

请注意,包括在GridDataSource上工作得很好,无论什么原因,我在DetailsDataSource上有问题。

我找到了一个解决方案,即检查关联引用是否在ToString覆盖中加载,如果没有加载它。

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

最新更新