开发者

Error Saving ViewState in Entity Framework

开发者 https://www.devze.com 2023-03-30 08:44 出处:网络
I am working in .NET Entity Framewor开发者_如何学Pythonk 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 er

I am working in .NET Entity Framewor开发者_如何学Pythonk 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]].'


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 :(.

0

精彩评论

暂无评论...
验证码 换一张
取 消