【发布时间】:2021-03-01 01:49:23
【问题描述】:
我有以下设置,但是当 MongoDB 集合有额外的字段时,程序会因错误而崩溃
"System.FormatException: 元素 'FriendName' 不匹配任何 MyApp.User 类的字段或属性"
我的印象是 MongoDB 驱动程序只能映射在 C# 类中声明的字段。有没有解决的办法 ?谢谢。
MongoDB - 集合User
{ Name: "Allen" , Age: 22, Address: "Sample Address", FriendName = "Sue"}
public class User
{
public string Name {get;set;}
public int Age {get; set;}
public string Address {get;set; }
}
_db.GetCollection<User>("User").Find(f => f.Name == "Allen").FirstOrDefault();
【问题讨论】: