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

OpenAccessContext.SaveOrUpdate

9 Answers 103 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Rui
Top achievements
Rank 1
Rui asked on 20 Mar 2012, 01:44 PM
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?

9 Answers, 1 is accepted

Sort by
0
Viktor Zhivkov
Telerik team
answered on 21 Mar 2012, 11:48 AM
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 >>
0
Rui
Top achievements
Rank 1
answered on 23 Mar 2012, 04:24 PM
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?
0
Viktor Zhivkov
Telerik team
answered on 27 Mar 2012, 03:08 PM
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 >>
0
Rui
Top achievements
Rank 1
answered on 27 Mar 2012, 06:03 PM
hello.
NHibernate supports this behavior:
http://stackoverflow.com/questions/1150854/saveorupdate-vs-update-and-save-in-nhibernate
0
Rui
Top achievements
Rank 1
answered on 28 Mar 2012, 10:16 AM

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();
0
Rui
Top achievements
Rank 1
answered on 28 Mar 2012, 10:18 AM
For the previous not to have problems in AutoMapper configuration didn't map the Association Properties and added
map.ForAllMembers(x => x.Ignore());

0
PetarP
Telerik team
answered on 30 Mar 2012, 03:17 PM
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 >>
0
Paresh
Top achievements
Rank 1
answered on 03 Sep 2014, 09:25 PM
has this feature been released ?


Thanks,
Paresh
0
Viktor Zhivkov
Telerik team
answered on 05 Sep 2014, 12:49 PM
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.
 
Tags
Development (API, general questions)
Asked by
Rui
Top achievements
Rank 1
Answers by
Viktor Zhivkov
Telerik team
Rui
Top achievements
Rank 1
PetarP
Telerik team
Paresh
Top achievements
Rank 1
Share this question
or