2011-08-25 33 views
0

我在.NET Entity Framework 4.0中工作 我正在使用viewstate来保存一个实体。我也序列化了这个实体。但是,当我尝试将数据保存到视图状态,收到此错误:在实体框架中保存ViewState时出错

Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Error serializing value 'System.Collections.Generic.List`1[Pc.PrecisionCare2.ModelTypes.Medication]' of type 'System.Collections.Generic.List`1[[Pc.PrecisionCare2.ModelTypes.Medication, PrecisionCare2ModelTypes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].' 
+1

是否有可能看到更多的代码? – Bobby

+0

我还要问,为什么你使用的视图状态,而不是会议?这取决于你的需求量的但我问的原因是因为我确定你已经知道这个,但ViewState的生命范围是当前页面,这意味着当你移动到另一个页面时,ViewState将被自动销毁。然而,Session对象存储在内存在Web服务器上,因此可用于任何页面,直到会话过期或应用程序会话终止,或在您的代码中手动销毁 – Bobby

+0

我正在将此与当前页面一起使用。问题是,我的实体也包含另一个实体。而且我没有序列化包含的实体。这就是发生这种错误的原因。我序列化包含的实体,现在一切正常。 – asma

回答

1

也许这将帮助任何人。我想自己在Viewstate中序列化一个实体,并找不到一个好的解决方案(XMLSerialization,Byte Serializing,DataContract)。我发现的是,我可以“扩展”代码生成的类(它们是部分的)并使其可序列化。

例如,这是一个.NET代码生成的实体:

//------------------------------------------------------------------------------ 
// <auto-generated> 
//  This code was generated from a template. 
// 
//  Manual changes to this file may cause unexpected behavior in your application. 
//  Manual changes to this file will be overwritten if the code is regenerated. 
// </auto-generated> 
//------------------------------------------------------------------------------ 

namespace OAuthServerBroker.EF 
{ 
    using System; 
    using System.Collections.Generic; 

    public partial class ResourceOwner 
    { 
     public ResourceOwner() 
     { 
      this.Grant = new HashSet<Grant>(); 
     } 

     public System.Guid ResourceOwner_ID { get; set; } 
     public string ResourceOwner_Username { get; set; } 

     public virtual ICollection<Grant> Grant { get; set; } 
    } 
} 

当我创建具有相同类名和命名空间,我可以让实体序列化:)一个新的类文件。

using System; 

namespace OAuthServerBroker.EF 
{ 
    [Serializable] 
    public partial class ResourceOwner 
    { 
     public EntityState State { get; set; } //can even put into new properties 
    } 
} 

希望这可以帮助任何人,因为它是一个古老的职位:(。