MongoDB が提供する C# ドライバーを使用して、MVC4 WebAPI で MongoDB データベースを使用しています。シリアル化に問題があります。次のエラーが表示されます。
"ExceptionMessage=Error getting value from '__emptyInstance' on 'MongoDB.Bson.ObjectId'"
HTTP リクエストで Content-Type を xml に変更すると、問題なく動作します。助けていただければ幸いです。
以下のモデルをコピーしました。
public class Subscriber
{
public ObjectId _id;
public long SubscriberId { get; set; }
public Name Name { get; set; }
public Address Address { get; set; }
public string Phone { get; set; }
public ICollection<Subscription> Subscription { get; set; }
public Subscriber()
{
Name = new Name();
Address = new Address();
Subscription = new Collection<Subscription>();
}
}
解決
_id タイプの文字列を変換し、以下のようにフィールドを装飾するとうまくいきました
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string _id;