This is a migrated thread and some comments may be shown as answers.

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

5 Answers 113 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Håkan
Top achievements
Rank 1
Håkan asked on 12 Aug 2012, 10:48 PM
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

5 Answers, 1 is accepted

Sort by
0
Ady
Telerik team
answered on 16 Aug 2012, 10:02 AM
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!
0
Çetin
Top achievements
Rank 1
answered on 14 Jan 2015, 11:31 PM
is it still not possible to update an object without load **now, in lastest version**? 
0
Ady
Telerik team
answered on 15 Jan 2015, 03:21 PM
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.
 
0
Çetin
Top achievements
Rank 1
answered on 17 Jan 2015, 11:50 AM
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;
        }
    }
0
Ady
Telerik team
answered on 20 Jan 2015, 01:53 PM
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.
 
Tags
Getting Started
Asked by
Håkan
Top achievements
Rank 1
Answers by
Ady
Telerik team
Çetin
Top achievements
Rank 1
Share this question
or