当尝试将继承与MongoRepository for C#结合使用时,我遇到了序列化错误.
真正奇怪的是它可以在很短的时间内工作,但是在说重建或其它失败之后.如果我删除该集合并创建一个新集合,它将一直有效,直到停止或重建.
我的代码看起来像:
public class Organization { // other attributes removed for demonstration simplicity public ListPeople { get;set; } } public abstract class Person { public string Id {get;set;} public string Name {get;set;} } public class Employee : Person { public string Badge {get;set;} } public class Contractor : Person { public string Company {get;set;} }
当我试图让它像:
static MongoRepositoryapps = new MongoRepository (); return apps.Single(c => c.Id == id);
我收到的错误是:
MongoDB.Driver.dll中出现"System.IO.FileFormatException"类型的异常,但未在用户代码中处理
附加信息:反序列化API类的People属性时发生错误.Models.Organization:无法创建抽象类的实例.
amcdnl.. 14
添加装饰器属性:
[BsonKnownTypes(typeof(Contractor), typeof(Employee))]
到班级解决了这个问题.