I have inheritance in my mongo domain objects:
public abstract class Entity : IEntity
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonIgnore]
public abstract string CollectionName { get; }
}
[BsonDiscriminator(Required = true)]
[BsonKnownTypes(typeof(Sub1), typeof(Sub2), typeof(Sub3))]
public class Main:Entity
{
public string Name {get; set;}
public string Address {get; set;}
}
[BsonDiscriminator(SubType.Sub1)]
public class Sub1: Main, IWorkAttached
{}
[BsonDiscriminator(SubType.Sub2)]
public class Sub2: Main, IWorkAttached
{}
[BsonDiscriminator(SubType.Sub3)]
public class Sub3: Main
{}
public interface IWorkAttached
{
public string WorkId {get; set;}
}
I have method for updating the entity:
public void Update(IWorkAttached entity)
{
var col = _db.GetCollection<IWorkAttached>("ColName");
var updDefinition = Builders<IWorkAttached>.Update
.Set(t => t.Name, entity.Name)
.Set(t => t.Address, entity.Address)
.Set(t => t.WorkId, entity.WorkId) // interface property
col.UpdateOne(t => t.Id == entity.Id, updDefinition)
}
The idea is to have one method for updating two types of entity(Sub1 and Sub2) and then I can use it for both entities:
var sub1 = new Sub1 {...};
var sub2 = new Sub2 {...};
Update(sub1);
Update(sub2);
But, it doesn't work. I get the following exception:
{document}.Id is not supported.
If I use specific type in the Update method instead of interface then all works fine. Is it possible to use mongo driver in this way?
Stacktrace:
at MongoDB.Driver.Linq.Translators.PredicateTranslator.GetFieldExpression(Expression expression)
at MongoDB.Driver.Linq.Translators.PredicateTranslator.TranslateComparison(Expression variableExpression, ExpressionType operatorType, ConstantExpression constantExpression)
at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node)
at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.MongoCollectionImpl`1.ConvertWriteModelToWriteRequest(WriteModel`1 model, Int32 index)
at System.Linq.Enumerable.<SelectIterator>d__154`2.MoveNext()
at MongoDB.Driver.Core.Operations.BulkMixedWriteOperation.BatchHelper.<FindOrderedRuns>d__8.MoveNext()
at MongoDB.Driver.Core.Misc.ReadAheadEnumerable`1.ReadAheadEnumerator.MoveNext()
at MongoDB.Driver.Core.Operations.BulkMixedWriteOperation.BatchHelper.<GetBatches>d__6.MoveNext()
at MongoDB.Driver.Core.Operations.BulkMixedWriteOperation.<ExecuteAsync>d__39.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MongoDB.Driver.OperationExecutor.<ExecuteWriteOperationAsync>d__3`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MongoDB.Driver.MongoCollectionImpl`1.<ExecuteWriteOperationAsync>d__62`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MongoDB.Driver.MongoCollectionImpl`1.<BulkWriteAsync>d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MongoDB.Driver.MongoCollectionBase`1.<UpdateOneAsync>d__47.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)