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
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:
- How to: Query Data
- How to: Insert Objects
- How to: Update Objects
- How to: Delete Objects
- How to: Save Changes
- How to: Handle Transactions
Kind regards,
Viktor Zhivkov
the Telerik team
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?
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,
the Telerik team
NHibernate supports this behavior:
http://stackoverflow.com/questions/1150854/saveorupdate-vs-update-and-save-in-nhibernate
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();
map.ForAllMembers(x => x.Ignore());
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
Thanks,
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