Update data without select it first

5 posts, 0 answers
  1. JC Wu
    JC Wu avatar
    15 posts
    Member since:
    Dec 2009

    Posted 06 Mar 2013 Link to this post

    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?
  2. Viktor Zhivkov
    Admin
    Viktor Zhivkov avatar
    291 posts

    Posted 11 Mar 2013 Link to this post

    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.
  3. DevCraft banner
  4. JC Wu
    JC Wu avatar
    15 posts
    Member since:
    Dec 2009

    Posted 11 Mar 2013 Link to this post

    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?
  5. Boris Georgiev
    Admin
    Boris Georgiev avatar
    190 posts

    Posted 14 Mar 2013 Link to this post

    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.
  6. JC Wu
    JC Wu avatar
    15 posts
    Member since:
    Dec 2009

    Posted 14 Mar 2013 Link to this post

    Got it, thanks.
Back to Top
DevCraft banner