Will SaveChanges inside Entity Framework wraps all changes in a database transaction
asp.net-mvc, asp.net-mvc-5, entity-framework, transactions
Solution
That is correct, SaveChanges acts like transaction meaning nothing is happening until you call it explicitly (nothing is sent to DB), and once you call it, everything is sent at once, and if one query/command fails, no changes will be persisted.
If you however want to send queries to SQL in batches, but still treat them as single transaction, you might look into what Entity Framework 6 has to offer (MSDN Article). Basically, you can wrap several SaveChanges in one big transaction, and if any of the queries/commands sent to SQL fails, in any of the batches, it will allow you to do a rollback of everything.
Problem
I have the following method inside my asp.net mvc web application, and i am using Ado.net entity framework to map my current database tables:- ``` public void changeDeviceSwitch(int fromID , int toID) { var currentdevices = tms.TMSSwitchPorts.Where(a => a.SwitchID == fromID); foreach (var d in currentdevices) { tms.TMSSwitchPorts.Remove(d); } foreach (var d in currentdevices) { TMSSwitchPort tsp = new TMSSwitchPort() { SwitchID = toID, TechnologyID = d.TechnologyID, PortNumber = d.PortNumber }; tms.TMSSwitchPorts.Add(d); } tms.SaveChanges(); } ``` My above method will generate multiple delete and add operations inside the database. so let say it will result in 5 delete operations and 5 insert operations, in this case will calling the SaveChangies() in my case, wraps the 10 operations in one database transaction ?, so either all changes happen or none of them ? Thanks