views:

622

answers:

1

Given two tables, Make and Model, where a make can contain many models and resulting in the following EF generated entity types...

/// <KeyProperties>
/// ID
/// </KeyProperties>
[global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName="CarsModel", Name="Make")]
[global::System.Runtime.Serialization.DataContractAttribute(IsReference=true)]
[global::System.Serializable()]
public partial class Make : global::System.Data.Objects.DataClasses.EntityObject
{
    /// <summary>
    /// Create a new Make object.
    /// </summary>
    /// <param name="id">Initial value of ID.</param>
    public static Make CreateMake(int id)
    {
        Make make = new Make();
        make.ID = id;
        return make;
    }
    /// <summary>
    /// There are no comments for Property ID in the schema.
    /// </summary>
    [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
    [global::System.Runtime.Serialization.DataMemberAttribute()]
    public int ID
    {
        get
        {
            return this._ID;
        }
        set
        {
            this.OnIDChanging(value);
            this.ReportPropertyChanging("ID");
            this._ID = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value);
            this.ReportPropertyChanged("ID");
            this.OnIDChanged();
        }
    }
    private int _ID;
    partial void OnIDChanging(int value);
    partial void OnIDChanged();
    /// <summary>
    /// There are no comments for Property Name in the schema.
    /// </summary>
    [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute()]
    [global::System.Runtime.Serialization.DataMemberAttribute()]
    public string Name
    {
        get
        {
            return this._Name;
        }
        set
        {
            this.OnNameChanging(value);
            this.ReportPropertyChanging("Name");
            this._Name = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, true);
            this.ReportPropertyChanged("Name");
            this.OnNameChanged();
        }
    }
    private string _Name;
    partial void OnNameChanging(string value);
    partial void OnNameChanged();
    /// <summary>
    /// There are no comments for Models in the schema.
    /// </summary>
    [global::System.Data.Objects.DataClasses.EdmRelationshipNavigationPropertyAttribute("CarsModel", "FK_Model_Make", "Model")]
    [global::System.Xml.Serialization.XmlIgnoreAttribute()]
    [global::System.Xml.Serialization.SoapIgnoreAttribute()]
    [global::System.Runtime.Serialization.DataMemberAttribute()]
    public global::System.Data.Objects.DataClasses.EntityCollection<Model> Models
    {
        get
        {
            return ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedCollection<Model>("CarsModel.FK_Model_Make", "Model");
        }
        set
        {
            if ((value != null))
            {
                ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.InitializeRelatedCollection<Model>("CarsModel.FK_Model_Make", "Model", value);
            }
        }
    }
}

/// <KeyProperties>
/// ID
/// </KeyProperties>
[global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName="CarsModel", Name="Model")]
[global::System.Runtime.Serialization.DataContractAttribute(IsReference=true)]
[global::System.Serializable()]
public partial class Model : global::System.Data.Objects.DataClasses.EntityObject
{
    /// <summary>
    /// Create a new Model object.
    /// </summary>
    /// <param name="id">Initial value of ID.</param>
    public static Model CreateModel(int id)
    {
        Model model = new Model();
        model.ID = id;
        return model;
    }
    /// <summary>
    /// There are no comments for Property ID in the schema.
    /// </summary>
    [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
    [global::System.Runtime.Serialization.DataMemberAttribute()]
    public int ID
    {
        get
        {
            return this._ID;
        }
        set
        {
            this.OnIDChanging(value);
            this.ReportPropertyChanging("ID");
            this._ID = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value);
            this.ReportPropertyChanged("ID");
            this.OnIDChanged();
        }
    }
    private int _ID;
    partial void OnIDChanging(int value);
    partial void OnIDChanged();
    /// <summary>
    /// There are no comments for Property Name in the schema.
    /// </summary>
    [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute()]
    [global::System.Runtime.Serialization.DataMemberAttribute()]
    public string Name
    {
        get
        {
            return this._Name;
        }
        set
        {
            this.OnNameChanging(value);
            this.ReportPropertyChanging("Name");
            this._Name = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, true);
            this.ReportPropertyChanged("Name");
            this.OnNameChanged();
        }
    }
    private string _Name;
    partial void OnNameChanging(string value);
    partial void OnNameChanged();
    /// <summary>
    /// There are no comments for Make in the schema.
    /// </summary>
    [global::System.Data.Objects.DataClasses.EdmRelationshipNavigationPropertyAttribute("CarsModel", "FK_Model_Make", "Make")]
    [global::System.Xml.Serialization.XmlIgnoreAttribute()]
    [global::System.Xml.Serialization.SoapIgnoreAttribute()]
    [global::System.Runtime.Serialization.DataMemberAttribute()]
    public Make Make
    {
        get
        {
            return ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedReference<Make>("CarsModel.FK_Model_Make", "Make").Value;
        }
        set
        {
            ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedReference<Make>("CarsModel.FK_Model_Make", "Make").Value = value;
        }
    }
    /// <summary>
    /// There are no comments for Make in the schema.
    /// </summary>
    [global::System.ComponentModel.BrowsableAttribute(false)]
    [global::System.Runtime.Serialization.DataMemberAttribute()]
    public global::System.Data.Objects.DataClasses.EntityReference<Make> MakeReference
    {
        get
        {
            return ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedReference<Make>("CarsModel.FK_Model_Make", "Make");
        }
        set
        {
            if ((value != null))
            {
                ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.InitializeRelatedReference<Make>("CarsModel.FK_Model_Make", "Make", value);
            }
        }
    }

I am trying to store an instance of the Make class into ASP.NET ViewState, along with the associated models as follows...

private Make Make {
  get { return this.ViewState[@"EditContext"] as Make; }
  set { this.ViewState[@"EditContext"] = value; }
}

and

this.Make = (from make in context.Makes.Include(@"Models")
             where make.ID == 1
             select make).FirstOrDefault();

But upon PostBack, this.Make.Models is always empty (depite definitely being populated when the entity was placed into the ViewState.

According to the MS Help,

Because entity types support binary serialization, objects can be saved in the view state of an ASP.NET application during a postback operation. When required, the object and its related objects are retrieved from the view state and attached to an existing object context.

I would therefore expect what I am doing to work. Can anybody explain why this is not the case?

Thanks.

A: 

It seems that the problem was my own fault. After adding the entity to the ViewState, I was manually detaching it from the object context. This was then clearing the links between the Make and the associated Model objects.

By simply adding the object to the ViewState without calling context.Detach(), the Models remain in the list, and I can still call context.Attach() to facilitate the manipulation of the object during the postback.

Martin Robins
Do you mean that you were detaching it before the Model entity set was populated, due to lazy loading? You could also resolve that with load options.
Kirk Broadhurst