Hello everyone. I work with mongoDb Driver 2.18.0
I have objects two different classes that implement the same interface. I store these objects in a mongo DB which works fine. The problem is the deserialization of this objects when I filter on interface properties.
My data modeling
// my interface
public interface IAudit
{
DateTime TimeStamp { get; set; }
string IpAddress { get; set; }
}
// first implementation
public class AuditEdi : IAudit
{
public DateTime TimeStamp { get; set; }
public string IpAddress { get; set; }
public string MessageType { get; set; }
....
}
// second implementation
public class AuditIhm : IAudit
{
public Guid UserId { get; set; }
public DateTime TimeStamp { get; set; }
public string IpAddress { get; set; }
...
}
Class Map registration
BsonClassMap.RegisterClassMap<AuditIhm>(cm =>
{
cm.SetDiscriminator("AuditIhm");
cm.AutoMap();
cm.SetIgnoreExtraElements(true);
});
BsonClassMap.RegisterClassMap<AuditEdi>(cm =>
{
cm.SetDiscriminator("AuditEdi");
cm.AutoMap();
cm.SetIgnoreExtraElements(true);
});
Try to filter on one of the interface’s property
var filter = Builders<IAudit>.Filter.Eq(r => r.IpAddress, ipAddress);
var sort = Builders<IAudit>.Sort.Descending("TimeStamp");
var findOptions = new FindOptions<IAudit>() {Sort = sort};
the exception
System.InvalidOperationException : ‘Unable to determine the serialization information for r => r.IpAddress.’
Should i use class inheritance instead of interface for modeling my documents or is it possible to use this implementation ?