【问题标题】:dbml Serialzable= Unidirection, but 'System.Data.Linq.EntityRef' still not mark as Serialzabledbml Serializable= 单向,但“System.Data.Linq.EntityRef”仍未标记为可序列化
【发布时间】:2011-04-04 05:29:23
【问题描述】:

我需要一个 linq-to-sql 使用 dbml,然后我按照这篇文章:http://www.west-wind.com/weblog/posts/147218.aspx

将子级设置为内部,将serialzable设置为单向并添加updatecheck=never

做所有的列

[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_DvdId",
  AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true,
  IsDbGenerated=true, UpdateCheck=UpdateCheck.Never)]
[global::System.Runtime.Serialization.DataMemberAttribute(Order=1)]

但我还是得到了

在程序集“System.Data.Linq, Version=4.0”中键入“System.Data.Linq.EntityRef`1[[CategoryList, App_Code.55_rpva1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]] .0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089' 未标记为可序列化。

在我的web.config 中,我使用二进制

<add name="Cart" type="ShoppingCart" serializeAs="Binary" allowAnonymous="true"/>

如果我不使用 linq,它肯定可以工作

请帮忙

【问题讨论】:

    标签: c# asp.net linq-to-sql serialization


    【解决方案1】:

    它是可序列化的,但根本不是通过BinaryFormatter*- 即它不是[Serializable]ISerializable

    LINQ 数据上下文设计为使用 DataContractSerializer 进行序列化,但是 - 对于二进制,NetDataContractSerializer 或 protobuf-net 也应该可以正常工作。

    如果这是 profile 序列化.... 老实说,我不知道您是否可以编写自己的插件序列化层 - 我从来不需要看;但在这种情况下,我会为我的配置文件中实际需要的对象属性编写一个简单的非 LINQ 对象(DTO),然后从 LINQ 填充该对象,然后序列化 那个


    *=对于它的价值,我不认为这是一件坏事; BinaryFormatter 有很多造成痛苦的方式,积极支持它实际上是一种积极的事情,因为最终让人们放弃了这种方法。

    【讨论】:

      【解决方案2】:

      使用限制器属性进行序列化对我有用:

      [Column]
      [DataMember]
      public int? MonitoredDirectorySubCategoryID { set; get; }
      
      [NonSerialized]
      [XmlIgnore]
      [IgnoreDataMember]
      private EntityRef<BasicReference> _MonitoredDirectorySubCategory = new EntityRef<BasicReference>();
      
      [XmlIgnore]
      [IgnoreDataMember]
      [Association(Name = "FK_FilRef_BasRef_MonitoredDirectorySubCategoryID", IsForeignKey = true, Storage = "_MonitoredDirectorySubCategory", ThisKey = "MonitoredDirectorySubCategoryID")]
      public BasicReference MonitoredDirectorySubCategory 
      { set { _MonitoredDirectorySubCategory.Entity = value; } get { return _MonitoredDirectorySubCategory.Entity; } }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-08
        • 2017-02-21
        • 2015-11-10
        • 2016-01-18
        • 2011-01-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多