MongoDB的登记类地图 [英] MongoDb registering class map

查看:178
本文介绍了MongoDB的登记类地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code。我想MiscellaneousData覆盖抽象Misc​​ellaneousDataBase。然而,IdMemberMap总是出现空。

使用一个独立的正常类的作品。

 如果(!BsonClassMap.IsClassMa pregistered(typeof运算(MiscellaneousData)))
{
    BsonClassMap.RegisterClassMap< MiscellaneousData>(厘米=>
    {
        cm.AutoMap();
        cm.SetIdMember(cm.GetMemberMap(C => c.Key));
        cm.IdMemberMap.SetIdGenerator(StringObjectIdGenerator.Instance);
    });
}
 

下面是杂项数据及其基础 - 降低下来净度:

 公共抽象类MiscellaneousDataBase
{
    [XmlIgnore]
    公共抽象的字符串键{获得;组; }
}

公共类MiscellaneousData:MiscellaneousDataBase
{
    公众覆盖字符串键{获得;组; }
}
 

解决方案

在创建类映射你的映射在声明它的水平的每个成员,所以你需要映射MiscellaneousDataBase和MiscellaneousData分开。

下面是一些示例code,展示了如何每个类分别映射(我添加了一个x属性的子类,所以会有一些在该级别映射)。

使用这些类:

 公共抽象类MiscellaneousDataBase
{
    公共抽象的字符串键{获得;组; }
}

公共类MiscellaneousData:MiscellaneousDataBase
{
    公众覆盖字符串键{获得;组; }
    公众诠释X {获得;组; }
}
 

尝试映射它们是这样的:

  BsonClassMap.RegisterClassMap< MiscellaneousDataBase>(厘米=>
{
    cm.AutoMap();
    cm.SetIdMember(cm.GetMemberMap(C => c.Key));
    cm.IdMemberMap.SetIdGenerator(StringObjectIdGenerator.Instance);
});

BsonClassMap.RegisterClassMap< MiscellaneousData>(厘米=>
{
    cm.AutoMap();
    cm.GetMemberMap(C => c.X).SetElementName(×);
});
 

I have the following code. I want MiscellaneousData to override an abstract MiscellaneousDataBase. However the IdMemberMap always comes up null.

Using a standalone 'normal' class works.

if (!BsonClassMap.IsClassMapRegistered(typeof(MiscellaneousData)))
{
    BsonClassMap.RegisterClassMap<MiscellaneousData>(cm =>
    {
        cm.AutoMap();
        cm.SetIdMember(cm.GetMemberMap(c => c.Key));
        cm.IdMemberMap.SetIdGenerator(StringObjectIdGenerator.Instance);
    });
}

Here is the miscellaneous data and its base - reduced down for clarity:

public abstract class MiscellaneousDataBase
{
    [XmlIgnore]
    public abstract string Key { get; set; }
}

public class MiscellaneousData : MiscellaneousDataBase
{
    public override string Key { get; set; }
}

解决方案

When creating class maps you map each member at the level at which it is declared, so you need to map MiscellaneousDataBase and MiscellaneousData separately.

Below is some sample code showing how to map each class separately (I added an X property to the subclass so there would be something to map at that level).

Using these classes:

public abstract class MiscellaneousDataBase
{
    public abstract string Key { get; set; }
}

public class MiscellaneousData : MiscellaneousDataBase
{
    public override string Key { get; set; }
    public int X { get; set; }
}

try mapping them like this:

BsonClassMap.RegisterClassMap<MiscellaneousDataBase>(cm =>
{
    cm.AutoMap();
    cm.SetIdMember(cm.GetMemberMap(c => c.Key));
    cm.IdMemberMap.SetIdGenerator(StringObjectIdGenerator.Instance);
});

BsonClassMap.RegisterClassMap<MiscellaneousData>(cm =>
{
    cm.AutoMap();
    cm.GetMemberMap(c => c.X).SetElementName("x");
});

这篇关于MongoDB的登记类地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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