Ejemplo n.º 1
0
        /// <summary>
        /// Basic test of the use of DbContext.Database.BeginTransaction()
        /// and use of SaveChanges() within the transaction.  It interacts
        /// directly with the DbContext.
        /// </summary>
        /// <returns>True if both updates are successful, otherwise False</returns>
        public bool Execute()
        {
            var context = new SiteMonitorDbDataContext();

            var settings1 = new Settings()
            {
                Id                = 0,
                EmailAccount      = "*****@*****.**",
                SlowResponseTime  = 1000,
                ScanFrequency     = FrequencyEnum.Daily,
                ArchiveRunDetails = new List <ArchiveDetail>()
            };

            var settings2 = new Settings()
            {
                Id                = 2,
                EmailAccount      = "*****@*****.**",
                SlowResponseTime  = 1000,
                ScanFrequency     = FrequencyEnum.Daily,
                ArchiveRunDetails = null
            };

            var success = false;

            using (var txn = context.Database.BeginTransaction())
            {
                try
                {
                    //  Insert settings1 record
                    context.MonitorSettings.Add(settings1);

                    var res1 = context.SaveChanges();
                    success = (res1 > 0);


                    if (success)
                    {
                        //  Insert settings2 record  if first was ok
                        context.MonitorSettings.Add(settings2);
                        var res2 = context.SaveChanges();
                        success = false; //res2 > 0;
                    }

                    if (success)
                    {
                        txn.Commit();
                    }
                    else
                    {
                        txn.Rollback();
                    }
                }
                catch
                {
                    txn.Rollback();
                }
            }

            return(success);
        }