我有一个Person
类和两个派生类Employee
和Manager
。
public class Person
{
[BsonElement("_Id")]
[BsonIgnoreIfDefault]
public ObjectId Id; // matches "_ID" in the database
public ContactEnum ContactType { get; set; }
public String FirstName { get; set; }
public String LastName { get; set; }
[BsonIgnore]
public String Notes { get; set; }
}
public class Employee : Person
{
public String Department { get; set; }
public Employee()
{
this.ContactType = ContactEnum.Employee;
}
}
public class Manager : Person
{
public String Group{ get; set; }
public Manager()
{
this.ContactType = ContactEnum.Manager;
}
}
当我尝试使用以下命令从MongoDB获取所有记录时:
public async Task<IEnumerable<Person>> GetContacts()
{
try
{
var items = await db.Person.Find(_ => true).ToListAsync();
return items;
}
catch (Exception ee)
{
throw;
}
}
我明白了:System.FormatException: 'Element 'Department ' does not match any field or property of class Models.Person.'
任何想法我该如何解决?