【问题标题】:MongoDB C# Driver: How to ignore property on Insert, but not in .Lookup()MongoDB C# 驱动程序:如何忽略 Insert 上的属性,但不在 .Lookup() 中
【发布时间】:2020-12-16 16:58:54
【问题描述】:

我有以下模型用于反序列化数据库中的集合:

[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],但是在应用查找时它会抛出错误:

'元素 'AlertType' 与 BAS.Models.AlertSettings.AlertSetting 类的任何字段或属性都不匹配。'

表示忽略发生在反序列化和序列化中..

这是一种我可以实现仅在插入或编辑时忽略属性的方法吗?

【问题讨论】:

    标签: c# mongodb linq driver bson


    【解决方案1】:

    我会使用 2 个不同的类来表示集合中的文档,一个来表示从聚合中查找的投影,因为这一个模型似乎用于多种目的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-21
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      • 2017-08-13
      • 1970-01-01
      相关资源
      最近更新 更多