我的 mongo 域对象有继承:
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;}
}
Run Code Online (Sandbox Code Playgroud)
我有更新实体的方法:
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)
}
Run Code Online (Sandbox Code Playgroud)
这个想法是有一种方法来更新两种类型的实体(Sub1和Sub2),然后我可以将它用于这两个实体:
var sub1 = new Sub1 {...};
var sub2 = new Sub2 {...};
Update(sub1);
Update(sub2);
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用。我得到以下异常:
不支持 {document}.Id。
如果我在方法中使用特定类型Update而不是接口,那么一切正常。是否可以以这种方式使用 mongo 驱动程序?
堆栈跟踪:
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10281 次 |
| 最近记录: |