Error Saving ViewState in Entity Framework

蹲街弑〆低调 提交于 2019-12-24 19:28:16

问题


I am working in .NET Entity Framework 4.0 I am using viewstate to save an entity. And I have serialize that entity as well. But when I try to save data to viewstate, getting this error:

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:


Maybe this will help anyone. I wanted to serialize a entity in the Viewstate myself and couldn't find a good solution (XMLSerialization, Byte Serializing, DataContract). What I found out is that I could "extend" the code generated classes (they are partial) and make it Serializable.

For example this is a .net code generated entity:

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

When I create a new class file with the same class name and namespace I can make the Entity Serializable :).

using System;

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

Hope this might help anyone since it's an old post :(.



来源:https://stackoverflow.com/questions/7186591/error-saving-viewstate-in-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!