Click here to Skip to main content
15,881,781 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have some tables in my database and want to insert records in these tables using Entity framework in MVC 4.0.
Insertion took place in this way that insertion of record in child table depends on the records in the master table.
This functionality could be achieve easily by Transaction and roll-back in the sql server. Where If record could not be insert in any of the table involved in the process then whole insertion process would roll-backed.

I want the alternative of this Transaction and roll-back with Entity framework in MVC 4.0.

Please provide some suggestions.

Thank you,

Regards,
Ashish
Posted
Comments
Herman<T>.Instance 19-Sep-14 7:10am    
what do you exactly mean by alternative?
Aashish vermaa 19-Sep-14 7:39am    
I means how could we code in Entity framework to achieve facility like roll-back in case of any issue.

Using Transactions you can solve your problms

C#
using System.Transactions; /Inculde this Directory and Reference


using (TransactionScope trans = new TransactionScope())
            {
                try
                {

                    using (DBEntities en = new DBEntities())
                    {
                        /*


			Yours DML(S)



			*/
		    }


                    trans.Complete();//If Success

                    
                }

                catch (Exception ex)
                {
                    trans.Dispose();//If Not Success
                    
                }
}
 
Share this answer
 
v3
Literally the first result from Google for "Entity Framework Transaction":

In all versions of Entity Framework, whenever you execute SaveChanges() to insert, update or delete on the database the framework will wrap that operation in a transaction. This transaction lasts only long enough to execute the operation and then completes. When you execute another such operation a new transaction is started.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900