2016-05-05 4 views
0

私はMongoDBを学んでいます。私はC#を使って試してみたいと思います。 C#公式MongoDBドライバを使用して強く型付けされたMongoDBドキュメントで操作することは可能ですか?mongoDB、C#の公式ドライバを使った強く型付けされたコレクション

私はクラスAlbumPhotoを持っている:

public class Album : IEnumerable<Photo> 
    { 
     [Required] 
     [BsonElement("name")] 
     public string Name { get; set; } 

     [Required] 
     [BsonElement("description")] 
     public string Description { get; set; } 

     [BsonElement("owner")] 
     public string Owner { get; set; } 

     [BsonElement("title_photo")] 
     public Photo TitlePhoto { get; set; } 

     [BsonElement("pictures")] 
     public List<Photo> Pictures { get; set; } 

     //rest of the class code 
    } 

public class Photo : IEquatable<Photo> 
    { 
     [BsonElement("name")] 
     public string Name; 

     [BsonElement("description")] 
     public string Description; 

     [BsonElement("path")] 
     public string ServerPath; 

     //rest of the class code 
    } 

私はtestデータベースに収集albumsに新しい文書を挿入します。私はBsonDocumentで操作したくないですが、強くタイプされたAlbumを使用することをお勧めします。私はそれはのようなものだろうと思った:

IMongoClient client = new MongoClient(); 
IMongoDatabase db = client.GetDatabase("test"); 
IMongoCollection<Album> collection = database.GetCollection<Album>("album"); 
var document = new Album 
      { 
       Name = album.Name, 
       Owner = HttpContext.Current.User.Identity.Name, 
       Description = album.Description, 
       TitlePhoto = album.TitlePhoto, 
       Pictures = album.Pictures 
      }; 
collection.InsertOne(document); 

しかし、それは私に次のエラー与える:私が間違っているのは何

An exception of type 'MongoDB.Driver.MongoCommandException' occurred in MongoDB.Driver.Core.dll but was not handled in user code

Additional information: Command insert failed: error parsing element 0 of field documents :: caused by :: wrong type for '0' field, expected object, found 0: [].

をし、それを達成することが可能かどう?

+0

[それは助けかもしれない](http://www.codeproject.com/Articles/273145/Using-MongoDB-with-the-Official-Csharp-Driver) – Chandralal

答えて

1

IEnumerable<Photo>を実装しているため、ドライバーがあなたのオブジェクトをBSONアレイとして扱っているようです。データベースは代わりにBSON文書を期待しています。たとえば、Int32をコレクションに挿入しようとすると、同様のエラーが発生します。

残念ながら、AlbumオブジェクトをBSONドキュメントとして扱うシリアライザの設定方法はわかりません。 BsonSerializer.SerializerRegistryという静的なプロパティは、ドライバがEnumerableInterfaceImplementerSerializer<Album,Photo>をデフォルトでAlbumのシリアライザとして使用することを示しています。

IEnumerable<Photo>の実装をAlbumから削除すると、ドライバはBsonClassMapSerializer<Album>でシリアル化され、BSONドキュメントが生成されます。それが機能する間、欠点はAlbumはもはや列挙できません。アプリケーションのコンシューマはPicturesプロパティを列挙する必要があります。バックにIEnumerable<Photo>実装を追加

は、その後に結果([BsonSerializer(typeof(BsonClassMapSerializer<Album>))]属性を使用して)前述のシリアライザを強制:スタックトレース(BsonSerializerAttribute.CreateSerializerを参照)に基づいて

System.MissingMethodException: No parameterless constructor defined for this object.

、オブジェクトは、メッセージが表示さを指しデータオブジェクト自体ではなく、何らかのシリアル化に関連するものである(私は両方のためのパラメータのないコンストラクタを定義した)。これ以上の設定でこの問題を回避する方法があるかどうか、またはドライバがこの方法でIEnumerableを使用できないようにするかどうかはわかりません。

+0

非常に建設的なコメントをありがとう。私はIEnumerableが問題であるとは知らなかった。現時点では、私はそのインターフェースを削除することで問題を解決しました。自分のイテレーターを実装しました。なぜなら、実際にはすべての連続した写真を取得する必要があるからです。 –

関連する問題