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.