Mongo C#驱动程序2.7-缺少_t鉴别符

时间:2018-10-14 16:39:34

标签: c# mongodb discriminator derived-types

更新到MongoDB C#官方驱动程序2.7版后,我的代码似乎已停止工作。 我有一个带有2个派生类的Nofitication类:NewFriendshipRequestNotification和FriendshipRequestAcceptedNotification。代码如下:

[CollectionName("Notification")]
[BsonKnownTypes(typeof(FriendshipRequestAcceptedNotification), typeof(NewFriendshipRequestNotification))]
public class Notification : MongoEntity
{
    [BsonRequired]
    [BsonRepresentation(BsonType.String)]
    public NotificationType Type { get; protected set; }

    [BsonRequired]
    public string TargetUserId { get; set; }

    [BsonRequired]
    public string Text { get; set; }

    [BsonRequired]
    public bool IsRead { get; set; } = false;

    [BsonIgnoreIfNull]
    public DateTime? ReadTimestamp { get; set; }

    public Notification()
    {
        Type = NotificationType.Generic;
    }
}

public class NewFriendshipRequestNotification : Notification
{
    [BsonRequired]
    public string RequestingUserId { get; set; }

    [BsonRequired]
    public string RequestingUsername { get; set; }

    public NewFriendshipRequestNotification()
    {
        Type = NotificationType.NewFriendshipRequest;
    }
}

public class FriendshipRequestAcceptedNotification : Notification
{
    [BsonRequired]
    public string AcceptingUserId { get; set; }

    [BsonRequired]
    public string AcceptingUsername { get; set; }

    public FriendshipRequestAcceptedNotification()
    {
        Type = NotificationType.FriendshipRequestAccepted;
    }
}

在此更新之前,由于基类的属性"_t": "NewFriendshipRequestNotification",当我插入NewFriendshipRequestNotification通知时,Notification集合中的文档具有一个[BsonKnownTypes]鉴别符字段。现在没有。 有人知道为什么吗?

0 个答案:

没有答案