MissingMethod Exception when trying to create a detached copy

2 posts, 0 answers
  1. Mikael
    Mikael avatar
    1 posts
    Member since:
    Apr 2016

    Posted 21 Apr Link to this post

    I get an error when trying to Detach a list of objects. The code looks like this:

     

    using (var context = new AltusContext())
    {
        IQueryable list = context.Addresses.Where(x => x.Datechecked == new DateTime(1900, 1, 1)).Take(10);
        IQueryable detached = context.CreateDetachedCopy(list);
        
    }

     

    I get a missing method exception, saying it cant find the constructor... any help would be appreciated.

    The error:

     

    System.MissingMethodException occurred
      Message=Le constructeur sur le type 'Telerik.OpenAccess.Query.Piece`1[[Altus.Data.Models.Geo.Address, Altus.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' est introuvable.
      Source=mscorlib
      StackTrace:
           à System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
           à System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
           à Telerik.OpenAccess.OpenAccessContext.CreateDetachedCopy[T](T entity, FetchStrategy fetchStrategy)
           à Telerik.OpenAccess.OpenAccessContext.CreateDetachedCopy[T](T entity, Expression`1[] referenceProperties)
           à Altus.UI.Test.TestForm.button4_Click(Object sender, EventArgs e)
      InnerException:

  2. Boris Georgiev
    Admin
    Boris Georgiev avatar
    190 posts

    Posted 28 Apr Link to this post

    Hi Mikael,

    This exception is thrown because you are trying to detach collection of objects directly from the database. To detach an object or collection of objects you should first load them in the memory. In your case you should call the ToList() method after the Take(10) method. The code should look like:
    using (var context = new AltusContext())
    {
        IQueryable list = context.Addresses.Where(x => x.Datechecked == new DateTime(1900, 1, 1)).Take(10).ToList();
        IQueryable detached = context.CreateDetachedCopy(list);
    }

    For more information how to Attach and Detach objects from OpenAccessContext you could refer to to this documentation article.

    I hope that helps.

    Regards,
    Boris Georgiev
    Telerik
     
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
  3. DevCraft banner
Back to Top