本文介绍了如何修复{document} .Id不支持的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的mongo域对象具有继承性:

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;}
}

我有更新实体的方法:

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)
}

这个想法是要有一种方法来更新两种类型的实体(Sub1Sub2),然后我可以将其用于两个实体:

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:

如果我在Update方法中使用特定类型而不是接口,则一切正常.这样可以使用mongo驱动程序吗?

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:

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)

推荐答案

我在项目中遇到了同样的问题,但是在另一种方法中,我的解决方案是编写完整的过滤器而不是使用t => t.Id == entity.Id:

I got same problem in my project, but in another method, and my solution is in writing full filter instead of using t => t.Id == entity.Id:

var filter = Builders<Account>.Filter.Eq("_id", new ObjectId(accountId));
account = _accountsCollection.Find(filter).First();

这篇关于如何修复{document} .Id不支持的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 02:11