2014-09-18 2 views
2

MongoDB에 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이 같은 프로토 파일에 메시지입니다 그들은 몇 문자열, 복식 및 int64s가 포함 개체는 다음과 같이 보인다. 나는이 같은 일부 데이터와 Measurement의 인스턴스를 작성하고 내 DB에 삽입하려고 :

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

을 지금, 나는이 DB의 내용을 확인하면, 나는 instanceOfMeasurement가 추가 된 것을 볼 수 있습니다 바르게. 그러나 반복되는 필드는 그렇지 않습니다. 더 복잡한 객체를 MongoDB에 채우기 전에 어떤 방법으로 준비해야합니까?

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

private string __someString1; 
[global::ProtoBuf.ProtoMember(1, IsRequired = false, [email protected]"_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, [email protected]"_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, [email protected]"_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, [email protected]"_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, [email protected]"_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); } 
} 

답변

2

드라이버는 아무 세터가없는 속성을 직렬화 기본적으로하지 않습니다 : 여기에 좋아

는 protobuf - 그물은 무엇을 생성합니다. 메시지에는 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에서 : 클래스가 자동 생성이 경우 속성은 나쁜 생각이 너무 후자를 사용하는 것 데이터베이스에 보관되었지만 결코 다시 읽지 않습니다. 이것은 "계산 된"속성을 저장하는 데 유용합니다.

관련 문제