1

I have several tables in the DB and I want to remove all the data and repopulate the tables and only then perform a save changes (because in case the save fails I want to return to the old data).

when I remove the data from the DB and then try to add the data into the DB it fails and says "Adding a relationship with an entity which is in the Deleted state is not allowed.", but when I remove the data then save and then add the new data and saves again, everything works fine..

here is my code if it helps understanding the problem

// create the new data
SomeDataHolder data = ... ; 
// save some data to re-enter back after changes
List<User> usersSave = ctx.Users.ToList(); 
List<UserPreferences> userPrefsSave = ctx.UserPreferences.ToList(); 

//clear DB
ctx.UserCourses.RemoveRange(ctx.UserCourses);
ctx.Users.RemoveRange(ctx.Users);
ctx.Specializtions.RemoveRange(ctx.Specializtions);
ctx.Course_Predecessor.RemoveRange(ctx.Course_Predecessor);
ctx.Courses.RemoveRange(ctx.Courses);
ctx.Departments.RemoveRange(ctx.Departments);
ctx.GroupsDetails.RemoveRange(ctx.GroupsDetails);
ctx.LinkTable.RemoveRange(ctx.LinkTable);

this next line makes everything works, without this line the code will fail on next save

// ctx.SaveChanges(); 

updateDepartmentsCoursesSpecialization(ctx, data.Specializations);
updateCoursePredecessorsAndParallel(ctx, data.Predecessors);
updateGroupDetails(ctx, data.GroupDetails);
updateLectureToPractice(ctx, data.LinkLectureWithPractice);
ctx.Users.AddRange(usersSave);
ctx.UserPreferences.AddRange(userPrefsSave);

ctx.SaveChanges();

1 Answer 1

3

Here you have to use Transaction.B'cos you're doing more than one atomic operation on your code base.By using Transaction where you can Combine several operations into one transaction within the same context.If there is any failure within the transaction then all will be roll-backed.

Transaction code snippet is like this :

 using (var ctx = new MyContext()) 
            { 
                using (var dbContextTransaction = ctx.Database.BeginTransaction()) 
                { 
                    try 
                    { 
                        //1st operations here
                        ctx.GroupsDetails.RemoveRange(ctx.GroupsDetails);
                        ctx.LinkTable.RemoveRange(ctx.LinkTable);
                        ctx.SaveChanges(); 

                        //2nd operations here
                        ctx.Users.AddRange(usersSave);
                        ctx.UserPreferences.AddRange(userPrefsSave);
                        ctx.SaveChanges();

                        dbContextTransaction.Commit(); 
                    } 
                    catch (Exception) 
                    { 
                        dbContextTransaction.Rollback(); 
                    } 
                } 
            } 

You can refer this for more info : Working with Transactions

Sign up to request clarification or add additional context in comments.

10 Comments

the problem still happens. in the same place. do I need to change the entity framework operations to SQL commands? or I can still use EF commands with this transactions
The same error occurs while trying to add items to the context.. got any idea? it occurs before the save changes in the "updateCoursePredecessorsAndParallel" function (which only do ctx.SomeTable.AddRange(SomeData) )
I believe its too long to post here ( but if you got any idea where I can send you the code I'll do) I can try and explain it , each update function you see receives a string matrix (data.something) with information and the context. the function then creates a list of the items from the string table into an object list and try to populate the db with the items without saving until the last update where you see the SaveChanges above.
Great! this is exactly the problem. Thanks!
Hi @Sampath, as I see this is already resolved, so all I can say is +1 :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.