Beispiel #1
0
 /// <summary>
 /// Called after any property is set. Sets IsDirty to <c>true</c>.
 /// </summary>
 public void MarkClean()
 {
     foreach (TableSchema.TableColumnSetting setting in columnSettings)
     {
         setting.IsDirty = false;
     }
     DirtyColumns.Clear();
 }
Beispiel #2
0
        /// <summary>
        /// Saves this object's state to the selected Database.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        public void Save(string userName)
        {
            bool isValid = true;

            if (ValidateWhenSaving)
            {
                BeforeValidate();

                isValid = Validate();
            }

            if (isValid)
            {
                if (IsNew)
                {
                    BeforeInsert();
                }
                else if (IsDirty)
                {
                    BeforeUpdate();
                }

                QueryCommand cmd = GetSaveCommand(userName);
                if (cmd == null)
                {
                    return;
                }

                // reset the Primary Key with the id passed back by the operation
                object pkVal = DataService.ExecuteScalar(cmd);

                // clear out the DirtyColumns
                DirtyColumns.Clear();

                if (pkVal != null)
                {
                    if (pkVal.GetType() == typeof(decimal))
                    {
                        pkVal = Convert.ToInt32(pkVal);
                    }

                    // set the primaryKey, only if an auto-increment
                    if (BaseSchema.PrimaryKey.AutoIncrement || BaseSchema.PrimaryKey.DataType == DbType.Guid)
                    {
                        try
                        {
                            SetPrimaryKey(pkVal);
                        }
                        catch
                        {
                            // this will happen if there is no PK defined on a table. Catch this and notify
                            throw new Exception("No Primary Key is defined for this table. A primary key is required to use SubSonic");
                        }
                    }
                }

                // set this object as old
                bool isNew = IsNew;
                MarkOld();
                MarkClean();
                AfterCommit(isNew);
            }
            else
            {
                // throw an Exception
                string notification = String.Empty;
                foreach (string message in Errors)
                {
                    notification += message + Environment.NewLine;
                }

                throw new Exception("Can't save: " + notification);
            }
        }