MongoDB C# 驱动程序:如何在插入时忽略 属性,而不是在 .Lookup() 中
MongoDB C# Driver: How to ignore property on Insert, but not in .Lookup()
我有以下模型用于反序列化数据库中的集合:
[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; }
}
属性 AlertType 和 Label 仅用于在应用 .Lookup() 时将其他集合中的对象反序列化为 Alert 对象。
所以我希望它们被忽略(插入、编辑等)。
我尝试添加属性 [BsonIgnore],但在应用 Lookup 时抛出错误:
'Element 'AlertType' does not match any field or property of class BAS.Models.AlertSettings.AlertSetting.'
意思是忽略发生在反序列化和序列化..
这是我可以实现仅在插入或编辑时忽略属性的方法吗?
我会使用 2 个不同的 类 来表示集合中的文档,一个来表示从聚合中查找的投影,因为这个模型似乎用于多种目的。
我有以下模型用于反序列化数据库中的集合:
[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; }
}
属性 AlertType 和 Label 仅用于在应用 .Lookup() 时将其他集合中的对象反序列化为 Alert 对象。
所以我希望它们被忽略(插入、编辑等)。
我尝试添加属性 [BsonIgnore],但在应用 Lookup 时抛出错误:
'Element 'AlertType' does not match any field or property of class BAS.Models.AlertSettings.AlertSetting.'
意思是忽略发生在反序列化和序列化..
这是我可以实现仅在插入或编辑时忽略属性的方法吗?
我会使用 2 个不同的 类 来表示集合中的文档,一个来表示从聚合中查找的投影,因为这个模型似乎用于多种目的。