/// <summary>
        /// Saves (or updates) the entity in the database.
        /// </summary>
        ///
        /// <param name="entity">
        /// Entity to save or update.
        /// </param>
        ///
        /// <param name="options">
        /// Optional options.
        /// </param>
        ///
        /// <returns>
        /// The number of affected rows.
        /// </returns>
        public override int Save(ref VahapYigit.Test.Models.UserRole entity, SaveOptions options = null)
        {
            ConditionChecker.Required(
                entity != null,
                new ArgumentNullException("entity"));

            if (options == null)
            {
                options = SaveOptions.Default;
            }

            int rowCount = 0;

            using (var scope = TransactionScopeHelper.CreateDefaultTransactionScope())
            {
                this.OnSaving(ref entity, options);

                if (options.SaveChildren)
                {
                    rowCount = this.PersistWithChildren(ref entity, new ObjectIDGenerator(), options);
                }
                else
                {
                    rowCount = this.Persist(ref entity, options);
                }

                this.OnSaved(ref entity, options);

                scope.Complete();
            }

            return(rowCount);
        }
示例#2
0
        protected DataSet ToDataSet(string procedure, IDictionary <string, object> parameters, out int returnValue, params string[] tableNames)
        {
            ConditionChecker.Required(
                !tableNames.IsNullOrEmpty(),
                new ArgumentNullException("tableNames"));

            DataSet dsResults = new DataSet()
            {
                RemotingFormat = SerializationFormat.Binary
            };

            SqlConnection dbConnection = null;
            SqlCommand    dbCommand    = null;

            try
            {
                dbConnection = this.GetSqlConnection(true);
                dbCommand    = new SqlCommand();

                dbCommand.CommandType = CommandType.StoredProcedure;
                dbCommand.CommandText = procedure;

                dbCommand.Connection = dbConnection;

                this.AddParameters(dbCommand, parameters);
                dbCommand.Parameters.Add(new SqlParameter("@ReturnValue", SqlDbType.Int)
                {
                    Direction = ParameterDirection.ReturnValue
                });

                using (var et = new ExecutionTracerService(procedure))
                    using (var dbAdapter = new SqlDataAdapter(dbCommand))
                    {
                        const string tName = "Table";
                        for (int i = 0; i < tableNames.Length; i++)
                        {
                            dbAdapter.TableMappings.Add((i == 0) ? tName : tName + i, tableNames[i]);
                        }

                        dbAdapter.Fill(dsResults);
                        returnValue = TypeHelper.To <int>(dbCommand.Parameters["@ReturnValue"].Value);
                    }
            }
            catch (Exception x)
            {
                if (this.OperationError != null)
                {
                    this.OperationError(this, new CrudOperationErrorEventArgs(x, procedure, parameters));
                }

                throw;
            }
            finally
            {
                dbCommand.SafeDispose();
                dbConnection.SafeDispose();
            }

            return(dsResults);
        }
        /// <summary>
        /// Refreshs the entity instance from the database.
        /// </summary>
        ///
        /// <param name="appSettings">
        /// Entity to refresh (must be in database).
        /// </param>
        public override void Refresh(ref VahapYigit.Test.Models.UserRole entity)
        {
            ConditionChecker.Required(
                entity != null,
                new ArgumentNullException("entity"));

            long id = entity.Id;

            entity = this.GetById(id);
            if (entity == null)
            {
                throw new EntityNotFoundException(VahapYigit.Test.Models.UserRole.EntityName, id);
            }
        }