When trying to combine inheritance with MongoRepository for C# I am experiencing serialization errors.
The really odd thing is it works for a short time but after say a rebuild or something it fails. If I drop the collection and create a new one it will work until a stop or rebuild.
My code looks like:
public class Organization
{
// other attributes removed for demonstration simplicity
public List<Person> People { 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;}
}
When I try to get it like:
static MongoRepository<Organization> apps = new MongoRepository<Organization>();
return apps.Single(c => c.Id == id);
The error I receive is:
An exception of type 'System.IO.FileFormatException' occurred in MongoDB.Driver.dll but was not handled in user code
Additional information: An error occurred while deserializing the People property of class API.Models.Organization: Instances of abstract classes cannot be created.
Adding the decorator attributes:
to the classes resolved the issue.