Is there a way to update object without loading it to context

6 posts, 0 answers
  1. Håkan
    Håkan avatar
    29 posts
    Member since:
    Apr 2012

    Posted 12 Aug 2012 Link to this post

    Hi All,

    We use to use Entity frame work most of the time, now we are going to shifted to OpenAccess and have good experience with it.
    But we face some kind of problems with openAccess as follows.

    If we need to Update or Edit an object what we did with EF is just change the state of the object and do the save changes.
    ex: 
         public void Update(Customer customer, Context context)  {
                try {
                    context.Customers.Add(customer);
                    context.Entry(customer).State = EntityState..Modified;
                    context.SaveChanges(); 
                    }catch (Exception exp) { throw exp; }
                }

    But in OpenAccess we feel that always we need to load object in to context before doing any update or delete.

    ex:
     public void UpdateCustomer(Customer customer)
            {
                try
                {
                    ObjectKey objectKey = new ObjectKey("Customer", new[] { new KeyValuePair<string, object>("ID", customer.ID) });
                    entity = context.Customers.Get(objectKey);
                              
                    entity.UpdatedOn = System.DateTime.Now;
                    entity.Name = customer.Name;
                    context.SaveChanges();
                }
                catch (Exception ex)  {  throw ex;   }
            }

    Is there a way to update object without loading it to context again like in EF.

    Development Team
  2. Ady
    Admin
    Ady avatar
    589 posts

    Posted 16 Aug 2012 Link to this post

    Hi Håkan,

     I have not tried using EF to achieve this but in OpenAccess, it is not possible to update an object without loading it. We need to load the object in order to detect the changes to the object and update only the changed fields in case there are no concurrent changes.

    Kind regards,
    Ady
    the Telerik team
    Follow @OpenAccessORM Twitter channel to be the first one to get the latest updates on new releases, tips and tricks and sneak peeks at our product labs!
  3. DevCraft banner
  4. Çetin
    Çetin avatar
    2 posts
    Member since:
    Apr 2014

    Posted 14 Jan 2015 in reply to Ady Link to this post

    is it still not possible to update an object without load **now, in lastest version**? 
  5. Ady
    Admin
    Ady avatar
    589 posts

    Posted 15 Jan 2015 Link to this post

    Hi Çetin ,

     Yes, it is now possible to update objects without loading them into the context first.

    This article describes how to use the UpdateAll method to update objects directly.

    Do get back in case you need further assistance.

    Regards,
    Ady
    Telerik
     
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
     
  6. Çetin
    Çetin avatar
    2 posts
    Member since:
    Apr 2014

    Posted 17 Jan 2015 in reply to Ady Link to this post

    Thank you for the answer? But How Can I add an "Generic Update Method into my Interface"
    Generic Update method must compere object by PK and update modified columns automaticly.
    is it possible in Telerik Data Access?

     public class Repository<T> : IDisposable, IRepository<T> where T : class
        {
            protected dbModel Context = new dbModel();
            public virtual IList<T> GetAll()
            {
                return Context.GetAll<T>().ToList();
            }

            public virtual T Get(Expression<Func<T, bool>> predicate)
            {
                return Context.GetAll<T>().FirstOrDefault(predicate);
            }

            public virtual T Get(object id)
            {
                var objectKey = new ObjectKey(typeof(T).Name, id);
                var entity = Context.GetObjectByKey(objectKey) as T;
                return entity;
            }

            public virtual void Add(T order)
            {
                Context.Add(order);
            }

            public virtual void Remove(T order)
            {
                Context.Delete(order);
            }

            public virtual void SaveChanges()
            {
                Context.SaveChanges();
            }

            public void Dispose()
            {
                Context = null;
            }
        }
  7. Ady
    Admin
    Ady avatar
    589 posts

    Posted 20 Jan 2015 Link to this post

    Hi Çetin,

     I'm afraid that is not possible to update fields in an generic way using the UpdateAll method .

    Regards,
    Ady
    Telerik
     
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
     
Back to Top
DevCraft banner