在实体框架中保存ViewState时出错 [英] Error Saving ViewState in Entity Framework

查看:133
本文介绍了在实体框架中保存ViewState时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在.NET Entity Framework 4.0中工作
我正在使用viewstate保存一个实体。而且我也把这个实体序列化了。但是当我尝试保存数据到viewstate,得到这个错误:

  Microsoft JScript运行时错误:Sys.WebForms.PageRequestManagerServerErrorException:错误序列化'System.Collections.Generic.List`1类型的System.Collections.Generic.List`1 [Pc.PrecisionCare2.ModelTypes.Medication]'[[Pc.PrecisionCare2.ModelTypes.Medication,PrecisionCare2ModelTypes,Version = 1.0。 0.0,Culture = neutral,PublicKeyToken = null]]'


解决方案

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



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

  // ----------------------- -------------------------------------------------- ----- 
//<自动生成>
//该代码是从模板生成的。
//
//手动更改此文件可能会导致应用程序出现意外的行为。
//如果重新生成代码,则对该文件的手动更改将被覆盖。
//< / auto-generated>
// -------------------------------------------- ----------------------------------

命名空间OAuthServerBroker.EF
{
using System;
使用System.Collections.Generic;

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

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

public virtual ICollection&Grant>授予{get;组; }
}
}

当我创建一个新的类文件类名和命名空间我可以使Entity Serializable:)。

  using System; 

命名空间OAuthServerBroker.EF
{
[Serializable]
public partial class ResourceOwner
{
public EntityState State {get;组; } //甚至可以放入新的属性
}
}

希望这个可能会帮助任何人,因为它是一个旧帖子:(。


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]].'

解决方案

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

这篇关于在实体框架中保存ViewState时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆