I have the following model that I use to deserialize a collection from the database:
[BsonCollection("alerts")]
public class Alert
{
public ObjectId Id { get; set; }
public string Name { get; set; }
[BsonRepresentation(BsonType.ObjectId)]
public string AlertTypeId { get; set; }
[BsonRepresentation(BsonType.ObjectId)]
public string Label Id { get; set; }
public AlertType AlertType { get; set; }
public Label Label { get; set; }
}
Properties AlertType and Label are only meant to be used to deserialize objects from other collections into Alert object when applying a .Lookup().
So I would want them to be ignored otherwise (insert, edit, etc.).
I tried adding attribute [BsonIgnore], but it throws an error when applying Lookup:
'Element 'AlertType' does not match any field or property of class BAS.Models.AlertSettings.AlertSetting.'
Meaning that the ignoring occures in deserialization and serialization..
Is this a way I can achieve ignoring the properties only when Inserting or Editing?
I'd use 2 different classes to represent the document in the collection and one to represent the projection with the lookup from an aggregation, as this one model seems to be used for multiple purposes.