【问题标题】:Serialization of EntityFramework LINQ ObjectsEntityFramework LINQ 对象的序列化
【发布时间】:2014-10-30 01:40:36
【问题描述】:

大师,

我想序列化 Entityframe Work 对象(底层对象来自 Oracle 表)。我正在使用 LINQ 查询。

代码片段

    using (Entities1 ctx = new Entities1())
    {

            ctx.Configuration.ProxyCreationEnabled = false;                    
    var oraLINQ1 = from ime in ctx.INV_MOVE_EVENT
                               select new
                               {
                                   ime.TO_POS_SLOT
                               };
                var bformatter = new BinaryFormatter();
                Stream stream = File.Open("INVMOVEEVENT.ifl", FileMode.Create);
                bformatter.Serialize(stream, oraLINQ1);
                stream.Close();
       }

我得到一个例外

"Type 'System.Data.Entity.Infrastructure.DbQuery`1[[<>f__AnonymousType0`1[[System.String, 
 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], EntityFramework,
 Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' in Assembly 'EntityFramework,
 Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as 
 serializable."}    System.Exception {System.Runtime.Serialization.SerializationException}

我尝试了继承 ISerializable 的 INV_MOVE_EVENT 实体类。它没有用。非常感谢任何指向正确方向的线索。

【问题讨论】:

  • 所以TO_POS_SLOT 是实体?因为您正在尝试序列化 TO_POS_SLOT 而不是 INV_MOVE_EVENT 的集合
  • 感谢您的回复。我不清楚。你能帮我纠正一下代码吗?
  • TO_POS_SLOT是什么类型的对象?
  • TO_POS_SLOT 是 IM_MOVE_EVENT 类中的字符串成员。public string TO_POS_SLOT { get;放; }
  • 有人能帮我解决这个问题吗?

标签: linq object serialization


【解决方案1】:

我这边无法测试,但您是否尝试将查询转换为列表?

 using (Entities1 ctx = new Entities1())
    {

            ctx.Configuration.ProxyCreationEnabled = false;                    
    var oraLINQ1 = from ime in ctx.INV_MOVE_EVENT
                               select new
                               {
                                   ime.TO_POS_SLOT
                               };
                var bformatter = new BinaryFormatter();
                Stream stream = File.Open("INVMOVEEVENT.ifl", FileMode.Create);
                bformatter.Serialize(stream, oraLINQ1.ToList());
                stream.Close();
       }

它应该可以正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 2012-09-03
    • 1970-01-01
    相关资源
    最近更新 更多