通用列表未插入MongoDB

时间:2014-09-18 12:21:16

标签: c# .net mongodb protobuf-net mongodb-.net-driver

我正在尝试将protobuf-net对象插入到MongoDB中。该对象看起来像这样:

message Measurement
{
    optional string                 _someString1            = 1 [default = ""];
    optional string                 _someString2            = 2 [default = ""];
    repeated MyMessage1             _myMessages1            = 3;
    repeated MyMessage2             _myMessages2            = 4;
    repeated MyMessage3             _myMessages3            = 5;
}

MyMessage1..3是同一个proto文件中的消息,它们包含一些字符串,双精度数和int64。 我用一些数据填充Measurement的实例并尝试将其插入到我的数据库中,如下所示:

var col = MyDatabase.GetCollection<Measurement>("Measurements");
col.Insert(instanceOfMeasurement);

现在,如果我查看数据库的内容,我可以看到instanceOfMeasurement已正确添加。但内部重复的领域却没有。在将其填充到MongoDB中之前,我是否需要以某种方式准备更复杂的对象?

好的,这就是protobuf-net创造的:

[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Measurement")]
public partial class Measurement : global::ProtoBuf.IExtensible, global::System.ComponentModel.INotifyPropertyChanged
{
public Measurement() {}

private string __someString1;
[global::ProtoBuf.ProtoMember(1, IsRequired = false, Name=@"_someString1", DataFormat = global::ProtoBuf.DataFormat.Default)]
public string _SomeString1
{
  get { return __someString1?? ""; }
  set { __someString1 = value; OnPropertyChanged(@"_someString1"); }
}
[global::System.Xml.Serialization.XmlIgnore]
[global::System.ComponentModel.Browsable(false)]
public bool _someString1Specified
{
  get { return this.__someString1 != null; }
  set { if (value == (this.__someString1== null)) this.__someString1 = value ? this._someString1 : (string)null; }
}
private bool ShouldSerialize_someString1() { return _someString1Specified; }
private void Reset_someString1() { _someString1Specified = false; }

private string __someString2;
[global::ProtoBuf.ProtoMember(2, IsRequired = false, Name=@"_someString2", DataFormat = global::ProtoBuf.DataFormat.Default)]
public string _SomeString2
{
  get { return __someString2?? ""; }
  set { __someString2 = value; OnPropertyChanged(@"_someString2"); }
}
[global::System.Xml.Serialization.XmlIgnore]
[global::System.ComponentModel.Browsable(false)]
public bool _someString2Specified
{
  get { return this.__someString2 != null; }
  set { if (value == (this.__someString2== null)) this.__someString2 = value ? this._someString2 : (string)null; }
}
private bool ShouldSerialize_someString2() { return _someString2Specified; }
private void Reset_someString2() { _someString2Specified = false; }

private readonly global::System.Collections.Generic.List<MyMessage1> __myMessages1 = new global::System.Collections.Generic.List<MyMessage1>();
[global::ProtoBuf.ProtoMember(6, Name=@"_myMessages1", DataFormat = global::ProtoBuf.DataFormat.Default)]
public global::System.Collections.Generic.List<MyMessage1> _myMessages1
{
  get { return __myMessages1; }
}

private readonly global::System.Collections.Generic.List<MyMessage2> __myMessages2 = new global::System.Collections.Generic.List<MyMessage2>();
[global::ProtoBuf.ProtoMember(7, Name=@"_myMessages2", DataFormat = global::ProtoBuf.DataFormat.Default)]
public global::System.Collections.Generic.List<MyMessage2> _myMessages2
{
  get { return __myMessages2; }
}

private readonly global::System.Collections.Generic.List<MyMessage3> __myMessages3 = new global::System.Collections.Generic.List<MyMessage3>();
[global::ProtoBuf.ProtoMember(8, Name=@"_myMessages3", DataFormat = global::ProtoBuf.DataFormat.Default)]
public global::System.Collections.Generic.List<MyMessage3> _myMessages3
{
  get { return __myMessages3; }
}

public event global::System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
  { if(PropertyChanged != null) PropertyChanged(this, new global::System.ComponentModel.PropertyChangedEventArgs(propertyName)); }

private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
  { return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
}

1 个答案:

答案 0 :(得分:2)

默认情况下,驱动程序没有序列化没有setter的属性。显然,这包括Measurement上的消息。

要覆盖默认行为并包含这些属性*,您可以使用BsonElement属性标记它们,也可以使用RegisterClassMap映射它们。在自动生成类的情况下,属性是个坏主意所以使用后者:

BsonClassMap.RegisterClassMap<Measurement>(m =>
{
    m.MapProperty(x => x.MyMessage1);
    m.MapProperty(x => x.MyMessage2);
    m.MapProperty(x => x.MyMessage3);
});

Serialize Documents with the C# Driver: Opt In

中的更多内容
  

*当readonly属性被序列化时,它的值将持久保存到数据库,但永远不会读回来。这对于存储“计算”属性很有用   功能