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

Update data without select it first

4 Answers 78 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
JC Wu
Top achievements
Rank 1
JC Wu asked on 07 Mar 2013, 02:41 AM
We can update a row by these code.
          var application = context.Applications.Single(app=> app.App_code == "123");
          application.App_name = "test";
          context.SaveChanges();

In microsoft entity framework, we can use Attach to do this update without select first.
         var application = new Applications();
         application.App_Code = "123";
         context.Applications.Attach(application);
         application.App_name = "test";
         context.SubmitChanges();

How can I do this in telerik ORM?

4 Answers, 1 is accepted

Sort by
0
Viktor Zhivkov
Telerik team
answered on 11 Mar 2013, 06:03 PM
Hi JC Wu,

In order to update an existing entity you need to load it in memory first.
This can be achieved using various operations like the first LINQ query that you have posted.
OpenAccess does not allow creation of new entity instances and using them to update existing one.
If you want to mark one to be updated via the Attach mechanism you will have to detach the same entity first. 
For more information about our attach-detach functionality please see this online article.

If you have any further questions, do not hesitate to contact us again.

Kind regards,
Viktor Zhivkov
the Telerik team
OpenAccess ORM Q1 2013 is out featuring Multi-Diagrams, Persistent Data Stream Support and much more. Check out all of the latest highlights.
0
JC Wu
Top achievements
Rank 1
answered on 12 Mar 2013, 02:44 AM
Thanks for reply.
In the online article, when detach
public void DetachObjectGraph(EntitiesModel dbContext)
{
Car carToDetach = dbContext.Cars.FirstOrDefault();
Car detachedCopy = dbContext.CreateDetachedCopy(carToDetach, car => car.Category, car => car.RentalOrders);
}

So it's necessary to get entity first?
0
Boris Georgiev
Telerik team
answered on 14 Mar 2013, 04:02 PM
Hello, 

Yes, is necessarily to have loaded in the cache the entity that you want to detach from the context. By design OpenAccess uses the entity for the root of the graph which will be detached from the current context - that is why needs to be loaded.

Note that only the primary key values are loaded for an object by default (with FirstOrDefault()), so CreateDetachedCopy method will also internally retrieve any persistent fields from the database that are not cached yet.

I hope that helps. Do not hesitate to contact as again if you have any other questions.

Kind regards,
Boris Georgiev
the Telerik team
OpenAccess ORM Q1 2013 is out featuring Multi-Diagrams, Persistent Data Stream Support and much more. Check out all of the latest highlights.
0
JC Wu
Top achievements
Rank 1
answered on 15 Mar 2013, 12:45 AM
Got it, thanks.
Tags
General Discussions
Asked by
JC Wu
Top achievements
Rank 1
Answers by
Viktor Zhivkov
Telerik team
JC Wu
Top achievements
Rank 1
Boris Georgiev
Telerik team
Share this question
or