1

My Code

partial class User
{
    [OnSerializing]
    public void ClearPassword()
    {
        Password = null;
    }
}

Linq-to-SQL

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.[User]")]
[global::System.Runtime.Serialization.DataContractAttribute()]
public partial class User : INotifyPropertyChanging, INotifyPropertyChanged
{
    [global::System.Runtime.Serialization.OnSerializingAttribute()]
    [global::System.ComponentModel.EditorBrowsableAttribute(EditorBrowsableState.Never)]
    public void OnSerializing(StreamingContext context)
    {
        this.serializing = true;
    }
}

Result

Invalid attribute. Both 'Void ClearPassword()' and 'Void OnSerializing(System.Runtime.Serialization.StreamingContext)' in type 'AuthenticationManager.User' have 'System.Runtime.Serialization.OnSerializingAttribute'.

Now, did the engineers at Microsoft create a way for two separate blocks of code to fire some events on serialization independent of one another? Especially considering the fact that they hijack this event to set this.serializing = true?

Thanks in advance.

Matthew James Davis
  • 12,134
  • 7
  • 61
  • 90

1 Answers1

1

If your Password property is using a backing field, you can set it to NonSerialized

  [XmlIgnore]
  [ScriptIgnore]
  public string Password { get { return _password;}  set { _password = value; } }

  [NonSerialized]
  private string _password;
Community
  • 1
  • 1
jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • I tried getting in and manually adding this to Linq-to-SQL but it deleted it on compile. I want to continue using Linq-to-SQL. – Matthew James Davis Jan 04 '14 at 20:07
  • This works best when using something like Entity Framework Code First... It's probably a better idea to have separate objects for database interaction and for storing the structure to disk. – jessehouwing Jan 04 '14 at 20:11