C # improve the knowledge of ADO. NET object data model (3)-about rollback

Source: Internet
Author: User

Original

ADO. NET Entity Data Model, in fact, I do not like to use this, mainly in the case of complex queries, writing a lot of linq statements, of course, can also directly execute SQL statements, however, complex SQL statements cannot be executed because of connection problems.
But ADO. NET Entity has the advantage of being able to update database changes in real time and automatically update the model. This is also why I am using pure SQL statements, we still insist on using a lot of Entity Data Models.
These days, we plan to use the entity model to write a database operation for multi-Table sequential modification, that is, the previous table or record can be modified successfully before the next table or record can be modified. Rollback is involved here.
I originally thought there was a rollback mechanism, but I couldn't find it. I don't know if my information is incomplete.
I think the following methods can be used to solve the problem.
1. If it is the same Entities, it is best to modify the records of each table in sequence, and then run only one SaveChanges () at the end to achieve the goal of simultaneous submission.
For example, the table name is fictitious,

using (MyEntities aentity = new MyEntities()){    TAB_MEMBER tab1 = aentity.TAB_MEMBER.Where(o => o.ID == "123").FirstOrDefault();    tab1.TBM_ID = "234";    TAB_EMPLOYEE tab2 = aentity.TAB_EMPLOYEE.Where(o => o.ID == "id1").FirstOrDefault();    tab2.TBA_ID = "id2";    aentity.SaveChanges();}

2. You can save old records in a new instance. However, instead of referencing and assigning values, the fields and attributes of the old record are assigned to the new instance at a time. Save the modification. If an error occurs, assign the value of the new instance to the record to be modified and save it again.
The relationship is as follows:
Record 1 copy = record 1;
Bool r = save record 1;

Ifr)
{
Bool r2 = save record 2;
If (! R2)
{
Retained the copy of record 1 to overwrite the modification of record 1.
}
}
Currently, I will use these two methods to solve the rollback problem. Welcome to your discussion.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.