Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将C#对象映射到B文档_C#_Asp.net Mvc_Mongodb_Mapping - Fatal编程技术网

将C#对象映射到B文档

将C#对象映射到B文档,c#,asp.net-mvc,mongodb,mapping,C#,Asp.net Mvc,Mongodb,Mapping,我对MongoDB比较陌生。我有一个定义如下的对象 [BsonDiscriminator("user")] public Class BrdUser { [BsonId(IdGenerator = typeof(StringObjectIdGenerator))] public string ID { get; set; } [BsonElement("username")] public string UserNm { get; set; } [Bs

我对MongoDB比较陌生。我有一个定义如下的对象

[BsonDiscriminator("user")]
public Class BrdUser
{
    [BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
    public string ID { get; set; }

    [BsonElement("username")]
    public string UserNm { get; set; }

    [BsonElement("email")]
    public string EmailAdrs { get; set; }
    .
    .
    .
    public IList<Question> Questions{ get; set; } //<-- Un sure as to what Bson type should this be
}

我的问题是,映射时,应该使用什么属性,以便用户对象与问题对象反序列化。在C#Driver中没有
[b文档]
attibute。

我不确定您卡在哪里,但请尝试:

var brdDoc = new BrdUser ();
brdDoc.UserNm = "invisible";
brdDoc.EmailAdrs = "someone@womewhere.com";
brdDoc.Questions = new []{ new Question{ Title = "Q", Desciption = "Question to ask" } };

var bsonDocument = brdDoc.ToBsonDocument ();
var jsonDocument = bsonDocument.ToJson ();

Console.WriteLine (jsonDocument);
它将打印:

{ 
   "_id" : null, 
   "username" : "invisible", 
   "email" : "someone@womewhere.com", 
   "Questions" : [{ "_id" : null, "title" : "Q", "desc" : "Question to ask" }] 
}

您是否尝试序列化对象?结果是什么?我的问题是给IListSorry的属性是什么?我的问题是给问题列表的属性是什么?它是[BSOneElement]还是其他东西?您不必使用属性。尽管尝试吧。
{ 
   "_id" : null, 
   "username" : "invisible", 
   "email" : "someone@womewhere.com", 
   "Questions" : [{ "_id" : null, "title" : "Q", "desc" : "Question to ask" }] 
}