|
Hi yonbav,
Thanks for the bug report. I'm able to reproduce this behaviour (MongoDB .NET/C# v2.7.2). The following example will store in MongoDB without _t discriminator:
class Program
|
{
|
public class Parent
|
{
|
public IEnumerable<IChild> Children {get; set;}
|
}
|
public interface IChild {}
|
public class Child: IChild {}
|
static void Main(string[] args)
|
{
|
var mongoURL = new MongoUrl("mongodb://<host>:<port>");
|
var client = new MongoClient(mongoURL);
|
var database = client.GetDatabase("databaseName");
|
var collection = database.GetCollection<Parent>("collectionName");
|
var document = new Parent { Children = new [] { new Child () }};
|
collection.InsertOne(document);
|
}
|
}
|
This is a bug in the serialisation of individual items in the Children collection, where the driver is using the incorrect NomicalType for the items.
In the meantime, there are 2 workarounds that you could use, explicitly specify that the class requires a discriminator:
[BsonDiscriminator(Required=true)]
|
public class Child: IChild {}
|
Or, explicitly specify the type of the array:
var document = new Parent { Children = new IChild[] { new Child() }};
|
Regards,
Wan.
|