【问题标题】:Saving Entity Framework objects in ViewState在 ViewState 中保存实体框架对象
【发布时间】:2009-08-23 13:47:20
【问题描述】:

给定两个表,Make 和 Model,其中一个 make 可以包含许多模型并导致以下 EF 生成的实体类型...

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

我正在尝试将 Make 类的一个实例以及相关模型存储到 ASP.NET ViewState 中,如下所示...

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

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

但是在 PostBack 时,this.Make.Models 始终为空(尽管在将实体放入 ViewState 时肯定会填充。

根据MS Help

因为实体类型支持二进制 序列化,对象可以保存在 ASP.NET 的视图状态 回发期间的应用程序 手术。需要时,对象 及其相关对象被检索 从视图状态并附加到 现有对象上下文。

因此,我希望我正在做的事情能够奏效。谁能解释为什么不是这样?

谢谢。

【问题讨论】:

    标签: asp.net entity-framework serialization viewstate


    【解决方案1】:

    看来问题是我自己的错。 将实体添加到 ViewState 后,我手动将其从对象上下文中分离出来。然后清除了 Make 和关联的 Model 对象之间的链接。

    通过简单地将对象添加到 ViewState 而不调用 context.Detach(),模型保留在列表中,我仍然可以调用 context.Attach() 以方便在回发期间对对象的操作。

    【讨论】:

    • 您的意思是在填充模型实体集之前将其分离,因为延迟加载?你也可以通过加载选项来解决这个问题。
    • 你能用例子解释一下吗?谢谢,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多