元素"id"与嵌套类的错误的任何字段或属性都不匹配 [英] Element 'id' does not match any field or property of error with nested classes

查看:60
本文介绍了元素"id"与嵌套类的错误的任何字段或属性都不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下mongodb文档架构;

I've the following mongodb document schema;

{ 
    "_id" : ObjectId("5c9d34ff781318afb9e8ab43"), 
    "name" : "Name", 
    "slug" : "slug",
    "services" : {
        "subservice" : {
            "id" : NumberInt(37030)
        }
    }
}

然后我将我的班级定义为;

and then i define my classes as;

public class MainModel
{
    public ObjectId Id { get; set; }

    [BsonElement("name")]
    public string Name { get; set; }

    [BsonElement("slug")]
    public string Slug { get; set; }

    [BsonElement("services")]
    public ServicesDef Services { get; set; }

    public class ServicesDef 
    {
        [BsonElement("subservice")]
        public SubServiceDef SubService{ get; set; }

        public class SubServiceDef 
        {
            [BsonElement("id")]
            public int Id { get; set; }
        }
    }
}

但是当我查询文档时,会以某种方式出现

But somehow when I query the document;

var result = await Repository.FindAsync(x => x.Slug == slug);

该services.subservice.id未正确注册并获得

That services.subservice.id isn't properly registered and getting

元素'id'与SubServiceDef类的任何字段或属性都不匹配.

Element 'id' does not match any field or property of class SubServiceDef.

卡在这里并寻求建议.

我认为无法通过"Id"反序列化;属性,但似乎还有解决方案.

I think I'm having the same issue with cannot deserialize with the "Id" attribute but seems there is solution yet.

推荐答案

长话短说:这都是关于约定的.MongoDB .NET驱动程序公开了静态类 ConventionRegistry ,该类允许您注册自己的约定(更多 __ defaults __ 和 __ attributes __ .深入研究(驱动程序 github),您会发现它注册了一个非常有趣的约定:

Long story short: it's all about conventions. MongoDB .NET driver exposes static class ConventionRegistry which allows you to register your own conventions (more here). Additionally there are two "built-in" conventions __defaults__ and __attributes__. Digging deeper (driver github) you can find that it registers one quite interesting convention:

new NamedIdMemberConvention(new [] { "Id", "id", "_id" })

这意味着 id 成员将被视为常规BSON _id元素.

Which means that id members will be considered as regular BSON _id elements.

该如何解决?

您可以摆脱默认约定

ConventionRegistry.Remove("__defaults__");

但是,您会自动删除所有其他驱动程序约定,这很有风险.另外,您可以创建一个始终为空的假属性:

However automatically you will drop all the other driver conventions which is pretty risky. Alternatively you can create a fake property which will always be empty:

public class SubServiceDef
{
    [BsonElement("id")]
    public int Id { get; set; }

    [BsonId]
    public ObjectId FakeId { get; set; }
}

或者您可以只使用 BsonNoId 属性,其中

or you can just use BsonNoId attribute which

指定类的IdMember应该为空.

Specifies that the class's IdMember should be null.

[BsonNoId]
public class SubServiceDef
{
    [BsonElement("id")]
    public int Id { get; set; }
}

因此,约定将在类映射中将您的 id 设置为IdMember,但是在后期处理期间,此属性将强制IdMember为null,并且您的类将成功反序列化

So the convention will be setting your id as IdMember in class map but then during postprocessing this attribute will force IdMember to be null and your class will get deserialized succesfully

这篇关于元素"id"与嵌套类的错误的任何字段或属性都不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆