Untitled

                Never    
C#
       
        private void EditRequestAndOthersWithCommit(BifContext bifcontext)
        {
            Guid requestId = new Guid("some existing request id");
            var req = BifCtx.Requests.SingleOrDefault(o => o.RequestId == requestId);
            req.UserNotes = "blablabla";

            //this could be followed by another changes in db cotext
            //command bellow creates for all changes in db context relevant SQL equivalent commands and run them on DB in single transaction, 
            //the isolation level of the transaction is whatever isolation level the database provider considers its default setting
            //for our SQL server it is default commited at the and of transaction
            BifCtx.SaveChanges(); // so after this row everything is commited in DB and not possible to roll back!!!
        }

        private void EditCompanyUnitNAmeAndOthersWihtCommit(BifContext bifcontext)
        {
            Guid id = new Guid("some existing company unit name id");
            var cu = BifCtx.CompanyUnitNamesLocs.SingleOrDefault(o => o.CompanyUnitNameLocId == id &&  o.SnapshotId == 1);
            cu.CompanyUnitNameDescription = "some example company unit name";

            //this could be followed by another changes in db cotext
            //command bellow creates for all changes in db context relevant SQL equivalent and run them on DB in single transaction, 
            //the isolation level of the transaction is whatever isolation level the database provider considers its default setting
            //for our SQL server it is by default commited at the and of transaction
            BifCtx.SaveChanges(); // so after this row everything is commited in DB and not possible to roll back!!!
        }

        private void Dummycheckin(BifContext bifcontext)
        {
            //it is  updating snapshot == 0 and  deleting snapshot == 1, creating also history with snapshot bigger then 2
            //BifCtx.SaveChanges(); you can run commit, but in case of unsucesfull  Checkin  everthing will be commited before CheckInCompany   => incosistance in DB 
            Guid? token = new Guid("it doest matter");
            bifcontext.CheckInCompany(new Guid("some company unit id"), ref token);
            //inside CheckInCompany is db stored proc which run directly on DB and NOT on bifContext,  DB context is stored to db only if you run BifCtx.SaveChanges() 
            //DB proc is running default in single transaction if you dont do any measurement
            //can throw exception
        }

        private void EditCompanyUnitNAmeAndOthersNOCommit(BifContext bifcontext)
        {
            Guid id = new Guid("some existing company unit name id");
            var cu = BifCtx.CompanyUnitNamesLocs.SingleOrDefault(o => o.CompanyUnitNameLocId == id && o.SnapshotId == 1);
            cu.CompanyUnitNameDescription = "some example company unit name";

            //this could be followed by another changes in db cotext
        }








        //this func is example of some long process only sucesfull running of whole changes in db context will produce consistent data in db
        //problem is that it is always imediatelly commited without any chance to rollback something, it can crash whenever and it remains from the view of long process partially commited
        //WRONG usage of db context
        //this example uses only dbcontext without db procedures
        private void LongLastingProcess()
        {
            Random r = new Random();

            using (BifContext bifcontext = new BifContext())
            {
                //before this there can be lot of func editing db context 
                EditCompanyUnitNAmeAndOthersWihtCommit(bifcontext);//will be commited imediatelly
                //
                //again many functions editing context or another things
                //here is  chance to throw some kind of unexpected errors or maybe it will end because of some validation or it can be interupted somehow
                //
                if (r.Next(10) < 5)
                {
                    throw new Exception("some error");
                }

                EditCompanyUnitNAmeAndOthersWihtCommit(bifcontext); //will be commited imediatelly

                //here can be lot of func editing db context before this
                if (r.Next(10) < 5)
                {
                    throw new Exception("some error");
                }
            }
        }
























        //WRONG usage of db context
        //this example uses only dbcotext + db stored procedure
        private void LongLastingProcess2()
        {
            Random r = new Random();

            using (BifContext bifcontext = new BifContext())
            {
                //this scenario is again problem
                //you firstly update db context some record with snapshot id == 1 in EditCompanyUnitNAmeAndOthers, it s not yet in DB just in EF model in db context(you will not call BifCtx.SaveChanges() in func)
                //then you will call checkin functionality it will run directly on DB, it will delete snapshot id == 1 and and db proc is by default automatically commited
                //when you run bifcontext.SaveChanges(); at the and it will crash, because in DB model you are non existing record which was in meanwhile deleted in db proc
                
                //before this there can be lot of func editing db context 
                EditCompanyUnitNAmeAndOthersWihtCommit(bifcontext);
                //here is  chance to throw some kind of unexpected errors or maybe it will end because of some validation or it can be interupted somehow
                
                if (r.Next(10) < 5)
                {
                    throw new Exception("some error");
                }

                Dummycheckin(bifcontext);

                bifcontext.SaveChanges();
            }
        }



























        //good usage... everything is commited or rollback at once, no incosistance in db
        private void LongLastingProcess3()
        {
            Random r = new Random();

            using (BifContext bifcontext = new BifContext())
            {


                using (var dbContextTransaction = bifcontext.Database.BeginTransaction())
                {
                    try
                    {
                        EditCompanyUnitNAmeAndOthersNOCommit(bifcontext);
                        //here is  chance to throw some kind of unexpected errors or maybe it will end because of some validation or it can be interupted somehow

                        if (r.Next(10) < 5)
                        {
                            throw new Exception("some error");
                        }
                        bifcontext.SaveChanges(); // this will move db context changes to DB but not COMMITED!!!

                        Dummycheckin(bifcontext);
                        //you can do another db changes and calling bifcontext.SaveChanges();

                        //at the end
                        dbContextTransaction.Commit();//will permanently store db changes to db, if everything went fine 
                    }
                    catch (Exception)
                    {
                        dbContextTransaction.Rollback(); // or rollback
                    }
                }
            }
        }

Raw Text