OpenAccessContext.SaveOrUpdate

10 posts, 0 answers
  1. Rui
    Rui avatar
    30 posts
    Member since:
    Feb 2012

    Posted 20 Mar 2012 Link to this post

    Hello, I noticed that in OpenAccessContext is a Add method and a Delete, but there is no way to update or save.

    I want to have a Repository where I want to have the following methods in an Interface which wil be implemented using OpenAccess, nHibernate or EntityFramework depending on the needs.

    void Save(T entity);
    void Add(T entity);
    void Remove(T entity);

    But for this I need a way to say dbContext.SaveOrUpdate(entityXY).

    What do you recomend?
  2. Viktor Zhivkov
    Admin
    Viktor Zhivkov avatar
    291 posts

    Posted 21 Mar 2012 Link to this post

    Hello Rui,

    Thank you for your interest in OpenAccess ORM. 

    I would like to point out one subtle detail about working with ORM products - you do as many changes (updates, edits, deletes) to your data context and then you call SaveChanges() method in order to apply the changes back to the database.

    You are welcome to take a look at our documentation about CRUD operations using OpenAccess.
    There you can find detailed guidance about:
    If you have any questions don't hesitate to contact us.

    Kind regards,
    Viktor Zhivkov
    the Telerik team
    Telerik OpenAccess ORM Q1 2012 release is here! Check out what's new or download a free trial >>
  3. DevCraft banner
  4. Rui
    Rui avatar
    30 posts
    Member since:
    Feb 2012

    Posted 23 Mar 2012 Link to this post

    Hello, thank for reply.
    Imagine I have the following:

    class objTable
        property id get set
        property name get set
    end class


    now I have

    var o = new objTable()
    o.id=5
    o.name="testing something"


    I know for sure that in the DB there is the objTable with the ID value equals to 5, so I want to do SaveOrUpdate(o) and without another query to DB server save the data. In this case it would translate into a an update.

    Is this possible?
  5. Viktor Zhivkov
    Admin
    Viktor Zhivkov avatar
    291 posts

    Posted 27 Mar 2012 Link to this post

    Hello Rui,

    Unfortunately, the behavior that you describe is not supported by OpenAccess ORM and is not a usual feature of ORM products in general.
    There is no way for the data context to decide whether the object you have created using the new keyword exists in the database or not without querying the data server. By design OpenAccess will try to create and INSERT query and if there is already a row with the same Id it will fail due primary key constraint.

    This is a very important design decision that prevents many problems related to concurrency and data integrity.

    Greetings,

    Viktor Zhivkov
    the Telerik team
    Telerik OpenAccess ORM Q1 2012 release is here! Check out what's new or download a free trial >>
  6. Rui
    Rui avatar
    30 posts
    Member since:
    Feb 2012

    Posted 27 Mar 2012 Link to this post

    hello.
    NHibernate supports this behavior:
    http://stackoverflow.com/questions/1150854/saveorupdate-vs-update-and-save-in-nhibernate
  7. Rui
    Rui avatar
    30 posts
    Member since:
    Feb 2012

    Posted 28 Mar 2012 Link to this post


    For all you guys that are using AutoMapper, I used this approach, any comments?



    if (aTableDTO.IdTable > 0)
        table = FindBy(aTableDTO.IdTable);
                 
    table = Mapper.Map(aTableDTO,table);
    if(table.IDTable==0)
        DbContext.Add(table);
                     
    DbContext.SaveChanges();
  8. Rui
    Rui avatar
    30 posts
    Member since:
    Feb 2012

    Posted 28 Mar 2012 Link to this post

    For the previous not to have problems in AutoMapper configuration didn't map the Association Properties and added
    map.ForAllMembers(x => x.Ignore());

  9. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 30 Mar 2012 Link to this post

    Hi Rui,

     We are going to introduce a functionality much similar to what you have requested in one of our next releases. What it will essentially give you is the chance to have an detached object that can later be attached to an active context. If the context already contains the same object than the operation will be considered like an update.

    Kind regards,
    Petar
    the Telerik team
    Telerik OpenAccess ORM Q1 2012 release is here! Check out what's new or download a free trial >>
  10. Paresh
    Paresh avatar
    13 posts
    Member since:
    Jul 2014

    Posted 03 Sep 2014 in reply to PetarP Link to this post

    has this feature been released ?


    Thanks,
    Paresh
  11. Viktor Zhivkov
    Admin
    Viktor Zhivkov avatar
    291 posts

    Posted 05 Sep 2014 Link to this post

    Hi Paresh,

    We have implemented Attach-Detach API that may suit your needs a few releases ago.
    If you are using the latest version you should be able to try it and see if it fix your specific scenario.
    In short this API allows you to change the OpenAccessContext instance that is managing a persistent entity.
    Another way to use the API is to just attach either a new or existing entity instance and let Telerik Data Access decide if there is need for Insert or Update statement to persist the data. Please note that there can be executed an internal SELECT query to check if the object exists and what are the value of it's attributes in the database in order to determine it's state.
    If you are looking for an feature that does not do automatic SELECT statement, I am afraid that you will not find this API suitable.
    For more information and code snippets you can read the Attaching and Detaching Objects section in our online documentation.

    If you need any further assistance do not hesitate to contact us with your questions.

    Regards,
    Viktor Zhivkov
    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