Transaction submission for Entity framework

Source: Internet
Author: User

The behavior of a group of business processes is called a transaction. The business of this group can be processed successfully, and we can commit this transaction to save the results of your actions. The commit of a thing is to execute your method to commit the database, before the Sava is placed in the cache and not executed to the database.

    • 1. Using TransactionScope

This is the most likely way to commit an ADO transaction. You need to reference the System.Transactions namespace.

var invoicedetailoriginal = invoicedetailservice.query (m = = M.detailid = = 86).            FirstOrDefault ();                    using (TransactionScope transaction = new TransactionScope ()) {try {                    list<bl_invoice> invoicelst = new list<bl_invoice> ();                    list<bl_invoicedetail> detaillst = new list<bl_invoicedetail> (); var lst = invoiceservice.query (M = m.id > 49).                    ToList ();                        for (int n = 0; n < 3; n++) {Bl_invoice i = new Bl_invoice () {iswd = Lst[n]. ISWD, isazure = Lst[n]. Isazure, InvoiceNumber = Lst[n]. InvoiceNumber, invoicedate = DateTime.Now, Bittype = Lst[n]. Bittype, Bpexternalid = Lst[n]. Bpexternalid, CAExternalid = Lst[n]. Caexternalid, Amount = Lst[n]. Amount, ismethodsynced = False, METHOD_INCC = (n = = 1)?                                               "XXXX": "P", createddate = DateTime.Now}; Bl_invoicedetail d = new Bl_invoicedetail () {Bl_invo  Ice = i, BITREF32 = invoicedetailoriginal.bitref32, Mssku = (n = = 1) ? "Save 20 Chinese characters, save 20 characters, save 20 Chinese characters, save 20 Chinese characters, and save 20 Chinese characters."                            ": invoicedetailoriginal.mssku, Amount = Invoicedetailoriginal.amount,                            Tax = invoicedetailoriginal.tax, Deferralamount = Invoicedetailoriginal.deferralamount, Bitdate_from = invoicedetailoriginal.bitdate_from, bitdate_to = Invoice                Detailoriginal.bitdate_to,            CreatedDate = DateTime.Now};                        Invoiceservice.add (i);     Invoicedetailservice.add (d); To this step error, and then see Add invoice also not added on. This is the function of the transaction.                        Of course the Savachangge () of the entity can also be seen as a transaction.                    Transaction.complete ();                }} catch (Exception ex) {throw ex; } finally {transaction.                Dispose (); }
    • 2. Savechangges () and Unitofwork.commit ()

Insert one or two related lists. There is an error in the list, and the entire transaction is rolled back. Use of the reponsitory of the iunitofwork.

List<bl_invoice> Invoicelst =NewList<bl_invoice>(); List<BL_InvoiceDetail> Detaillst =NewList<bl_invoicedetail>(); varLST = invoiceservice.query (m = m.id > the).            ToList ();  for(intn =0; N < LST. Count (); n++) {Bl_invoice i=NewBl_invoice () {iswd=Lst[n]. ISWD, Isazure=Lst[n]. Isazure, InvoiceNumber=Lst[n]. InvoiceNumber, Invoicedate=DateTime.Now, Bittype=Lst[n]. Bittype, Bpexternalid=Lst[n]. Bpexternalid, Caexternalid=Lst[n]. Caexternalid, Amount=Lst[n]. Amount, ismethodsynced=false, METHOD_INCC= (n==1)?"XXX":"P", CreatedDate=DateTime.Now};                Invoicelst.add (i); varinvoicedetailoriginal = invoicedetailservice.query (m = M.detailid = = the). FirstOrDefault ();//Lst[n]. Bl_invoicedetail.firstordefault ();Bl_invoicedetail d =NewBl_invoicedetail () {Bl_invoice=I, BITREF32=Invoicedetailoriginal.bitref32, Mssku= (n = =1) ?"Save 20 Chinese characters, save 20 characters, save 20 Chinese characters, save 20 Chinese characters, and save 20 Chinese characters. ": Invoicedetailoriginal.mssku, Amount=Invoicedetailoriginal.amount, tax=Invoicedetailoriginal.tax, Deferralamount=Invoicedetailoriginal.deferralamount, Bitdate_from=Invoicedetailoriginal.bitdate_from, Bitdate_to=invoicedetailoriginal.bitdate_to, CreatedDate=DateTime.Now};            Detaillst.add (d); } invoiceservice.addrange (Invoicelst, detaillst);

In general, database operations in the Entity Framework all reflect the relevance of the transaction, which is also a reasonable requirement. Simpler and easier to operate than ADO.

Transaction submission for Entity framework

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.