Failing to retrieve nested objects in LiteDB

0

I have a problem while loading nested objects from the DB (inserting them works fine):

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(), new Audio(Guid.NewGuid(), "path2")));

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

Exception:

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, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, 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, Object args, 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, ContextCallback callback, Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, 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, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, 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')

I also tried using [BsonCtor] on the Sound constructor but I really don't know what might be causing this error.

So the problem seems to be that the constructor of Sound has Audio as a parameter, so how can I make it work? How can I make LiteDB return a List that also retrieves and generates from the DB the Audio object each Sound object contains?

Any help is really appreciated!

c#
database
wpf
nosql
litedb
asked on Stack Overflow Apr 28, 2021 by jotalanusse

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0