gpt4 book ai didi

c# - 通用列表未插入 MongoDB

转载 作者:可可西里 更新时间:2023-11-01 10:30:46 25 4
gpt4 key购买 nike

我正在尝试将一个 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 是同一原型(prototype)文件中的消息,它们包含一些字符串、 double 和 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); }
}

最佳答案

默认情况下,驱动程序不会序列化没有 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

* When a readonly property is serialized, it value is persisted to the database, but never read back out. This is useful for storing “computed” properties

关于c# - 通用列表未插入 MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25912478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com