微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法检索 LiteDB 中的嵌套对象

如何解决无法检索 LiteDB 中的嵌套对象

数据库加载嵌套对象时遇到问题(插入它们工作正常):

public class Audio
{
    public Guid Id { get; }
    public string Path { get; }

    public Audio(Guid id,string path)
    {
        Id = id;
        Path = path;
    }
}

public class Sound
{
    public Guid Id { get; }
    [BsonField("audio")]
    [BsonRef("audio")]
    public Audio Audio { get; set; }

    public Sound(Guid id,Audio audio)
    {
        Id = id;
        Audio = audio;
    }
}

ILiteCollection<Sound> soundCollection = db.GetCollection<Sound>("sound");

soundCollection.Insert(new Sound(Guid.NewGuid(),new Audio(Guid.NewGuid(),"path1")));
soundCollection.Insert(new Sound(Guid.NewGuid(),"path2")));

List<Sound> allSounds = new List<Sound>();
allSounds.AddRange(soundCollection.FindAll().ToList());

异常:

Prism.Ioc.ContainerResolutionException
  HResult=0x80131500
  Message=An unexpected error occurred while resolving 'SoundMod.viewmodels.MainWindowviewmodel'
  Source=Prism.Unity.Wpf
  StackTrace:
   at Prism.Unity.UnityContainerExtension.Resolve(Type type,ValueTuple`2[] parameters)
   at Prism.Unity.UnityContainerExtension.Resolve(Type type)
   at Prism.PrismInitializationExtensions.<>c.<ConfigureviewmodelLocator>b__0_0(Object view,Type type)
   at Prism.Mvvm.viewmodelLocationProvider.AutoWireviewmodelChanged(Object view,Action`2 setDataContextCallback)
   at Prism.Mvvm.viewmodelLocator.AutoWireviewmodelChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex,DependencyProperty dp,PropertyMetadata Metadata,EffectiveValueEntry oldEntry,EffectiveValueEntry& newEntry,Boolean coerceWithDeferredReference,Boolean coerceWithCurrentValue,OperationType operationType)
   at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp,Object value,OperationType operationType,Boolean isInternal)
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp,Object value)
   at Prism.Mvvm.viewmodelLocator.SetAutoWireviewmodel(DependencyObject obj,Nullable`1 value)
   at Prism.Common.MvvmHelpers.Autowireviewmodel(Object viewOrviewmodel)
   at Prism.prismapplicationBase.Initialize()
   at Prism.prismapplicationBase.InitializeInternal()
   at Prism.prismapplicationBase.OnStartup(StartupEventArgs e)
   at System.Windows.Application.<.ctor>b__1_0(Object unused)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback,Object args,Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source,Delegate callback,Int32 numArgs,Delegate catchHandler)
   at System.Windows.Threading.dispatcherOperation.InvokeImpl()
   at System.Windows.Threading.dispatcherOperation.InvokeInSecurityContext(Object state)
   at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback callback,Object state)
--- End of stack trace from prevIoUs location ---
   at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,Object state)
   at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext,Object state)
   at System.Windows.Threading.dispatcherOperation.Invoke()
   at System.Windows.Threading.dispatcher.ProcessQueue()
   at System.Windows.Threading.dispatcher.WndProcHook(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Boolean& handled)
   at MS.Win32.HwndSubclass.dispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback,Delegate catchHandler)
   at System.Windows.Threading.dispatcher.LegacyInvokeImpl(dispatcherPriority priority,TimeSpan timeout,Delegate method,Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.dispatchMessage(MSG& msg)
   at System.Windows.Threading.dispatcher.PushFrameImpl(dispatcherFrame frame)
   at System.Windows.Threading.dispatcher.PushFrame(dispatcherFrame frame)
   at System.Windows.Threading.dispatcher.Run()
   at System.Windows.Application.Rundispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run()
   at SoundMod.App.Main()

  This exception was originally thrown at this call stack:
    System.Linq.Expressions.Expression.New(System.Type)
    LiteDB.Reflection.CreateClass(System.Type)
    LiteDB.Reflection.CreateInstance(System.Type)

Inner Exception 1:
ResolutionFailedException: Resolution Failed with error: Exception has been thrown by the target of an invocation.

For more detailed information run Unity in debug mode: new UnityContainer().AddExtension(new Diagnostic())

Inner Exception 2:
TargetInvocationException: Exception has been thrown by the target of an invocation.

Inner Exception 3:
LiteException: Failed to create instance for type 'SoundMod.sound' from assembly 'SoundMod.sound,SoundMod,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null'. Checks if the class has a public constructor with no parameters.

Inner Exception 4:
ArgumentException: Type 'SoundMod.sound' does not have a default constructor (Parameter 'type')

我也尝试在 Sound 构造函数上使用 [BsonCtor],但我真的不知道是什么导致了这个错误

所以问题似乎是 Sound 的构造函数有 Audio 作为参数,那么我怎样才能让它工作呢? 如何让 LiteDB 返回一个列表,该列表也从 DB 中检索和生成每个 Sound 对象包含的 Audio 对象?

非常感谢任何帮助!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?