Пример #1
0
        public void Save(bool updateDatabase)
        {
            if (updateDatabase)
            {
                DateTime updateDate = DateTime.Now;

                switch (base.MyState)
                {
                case MyObjectState.New:
                    using (SqlConnection con = DBSettings.NewSqlConnectionOpen)
                        using (SqlTransaction trx = DBSettings.NewSqlTransaction("DefendantInsert", con))
                        {
                            try
                            {
                                Insert(trx, updateDate);

                                //After an insert, defendant child collections must have their the parentid
                                //updated with the new defendantid or they will get orphaned.

                                if (_Employers != null)
                                {
                                    _Employers.ParentId = this.ID;
                                    _Employers.SaveDefendantEmployers(trx);
                                }

                                if (_Plans != null)
                                {
                                    _Plans.ParentId = this.ID;
                                    _Plans.SaveDefendantPlans(trx);
                                }

                                trx.Commit();
                            }
                            catch
                            {
                                // rolling back the transaction and rethrowing base error.
                                trx.Rollback();
                                throw;
                            }
                        }
                    break;


                case MyObjectState.Modified:
                    using (SqlConnection con = DBSettings.NewSqlConnectionOpen)
                        using (SqlTransaction trx = DBSettings.NewSqlTransaction("DefendantUpdate", con))
                        {
                            try
                            {
                                if (HasChanged)
                                {
                                    Update(trx, updateDate);
                                }

                                if (_Employers != null)
                                {
                                    _Employers.SaveDefendantEmployers(trx);
                                }

                                if (_Plans != null)
                                {
                                    _Plans.SaveDefendantPlans(trx);
                                }

                                trx.Commit();
                            }
                            catch
                            {
                                // rolling back the transaction and rethrowing base error.
                                trx.Rollback();
                                throw;
                            }
                        }
                    break;
                }

                // updating audit variables in child collections
                if (HasChanged)
                {
                    this.SetNewUpdateProperties(LocalUser.WindowsUserName, updateDate);
                }

                _Employers.CleanCollection();
                _Plans.CleanCollection();
            }

            SaveBackups();
            base.MyState = MyObjectState.Current;
        }