2014-05-13 3 views
2

다음 클래스가 있습니다.객체의 MongoDB 직렬화/직렬화

public class User : System.Security.Principal.IPrincipal 
{ 
    public int _id { get; set; } 

    public IIdentity Identity { get; set; } 

    public bool IsInRole(string role) { }  
} 

나는 다음과 같은 코드를 사용하여 MongoDB를이 클래스의 인스턴스를 저장하려고 해요 : 나는 DB에서 개체를 검색 할 때, 저장 한 후

new MongoClient("") 
    .GetServer() 
    .GetDatabase("") 
    .GetCollection("") 
     .Save<User>(
      new User 
      { 
       _id = 101, 
       Identity = new GenericIdentity("uName", "Custom") 
      } 
     ); 

을; Identity.NameIdentity.AuthenticationType의 값은 여전히 ​​null입니다.

직렬화/직렬화 해제 지시 사항이 없기 때문입니까?

클래스 맵을 사용해 보았지만 문제는 동일하게 유지됩니다.

BsonClassMap.RegisterClassMap<User>(cm => 
{ 
    cm.AutoMap(); 
    cm.MapIdProperty(c => c._id); 
    cm.MapProperty(c => c.Identity); 
}); 

편집

이는 DB에 저장된 얻을 것입니다 :

{ 
    "_id" : 101, 
    "Identity" : { 
     "_t" : "GenericIdentity", 
     "Actor" : null, 
     "BootstrapContext" : null, 
     "Label" : null 
    } 
} 
+0

사용자를 저장 한 후에는 데이터베이스에서 어떻게 보입니까? –

+0

@CraigWilson, 질문이 업데이트되었습니다. – dan

+2

나는 문제가 우리가 GenericIdentity를 재수화할 수있는 방법이 없다고 생각한다. GenericIdentity 클래스의 클래스 맵을 등록하여 다시 만들 필요가있는 속성, 특히 Name과 AuthenticationType을 유지해야합니다. 그런 다음 작성자를 매핑하여 다시 생성해야합니다. –

답변

2

문제는 GenericIdentity은 데이터 클래스가되지 않는 것입니다 당신이 돈 특성을 많이 가지고 ' 계속 지키고 싶다. 이 경우 수동으로 매핑해야합니다. 아래에서는 실제로 중요한 두 가지 속성 인 Name과 AuthenticationType을 매핑하겠습니다. 그런 다음이 두 매개 변수를 사용하는 생성자를 사용하여 GenericIdentity를 생성하도록 MongoDB 드라이버에 지시합니다.

BsonClassMap.RegisterClassMap<GenericIdentity>(cm => 
{ 
    cm.MapProperty(c => c.Name); 
    cm.MapProperty(c => c.AuthenticationType); 
    cm.MapCreator(i => new GenericIdentity(i.Name, i.AuthenticationType)); 
}); 
+0

감사합니다. 당신은 천재입니다. – dan

+1

@dan 글쎄, 그는 드라이버를 만들지 않습니다. – i3arnon