如何解决无法检索 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 举报,一经查实,本站将立刻删除。