Esempio n. 1
0
        /// <summary>
        /// Get insert command.
        /// </summary>
        /// <param name="entities">A collection of entities.</param>
        /// <returns>Returns command.</returns>
        public virtual DbCommandContext GetInsertCommand(ICollection <TEntity> entities)
        {
            DbCommand command = _storageContext.CreateCommand();

            command.CommandText = _queryBuilder.GetInsertSql();

            if (_storageContext.TransactionExists)
            {
                command.Transaction = _storageContext.GetDbTransactionContext().Transaction;
            }

            DbCommandContext cmdContext = new DbCommandContext(command, entities.Cast <IEntity>().ToList());

            cmdContext.SetParametersForEach <TEntity>((parameters, entity) =>
            {
                foreach (PropertyConfiguration pc in _configuration.PropertyConfigurations)
                {
                    if (pc.IsKey && !_configuration.HasCompositeKey)
                    {
                        continue;
                    }

                    parameters[pc.PropertyName].Value = GetPropertyValue(entity, pc);
                }
            },
                                                      // For retrieving last inserted id
                                                      (_configuration.KeyPropertyConfigurations[0].IsIntegerKey) ?
                                                      _configuration.KeyPropertyConfigurations[0] : null);

            return(cmdContext);
        }
Esempio n. 2
0
        /// <summary>
        /// Get delete command.
        /// </summary>
        /// <param name="entities">A collection of entities.</param>
        /// <returns>Returns command.</returns>
        public virtual DbCommandContext GetDeleteCommand(ICollection <TEntity> entities)
        {
            DbCommand command = _storageContext.CreateCommand();

            command.CommandText = _queryBuilder.GetDeleteSql();

            if (_storageContext.TransactionExists)
            {
                command.Transaction = _storageContext.GetDbTransactionContext().Transaction;
            }

            DbCommandContext cmdContext = new DbCommandContext(command, entities.Cast <IEntity>().ToList());

            cmdContext.SetParametersForEach <TEntity>((parameters, entity) =>
            {
                foreach (PropertyConfiguration pc in _configuration.KeyPropertyConfigurations)
                {
                    parameters[pc.PropertyName].Value = GetPropertyValue(entity, pc);
                }
            });

            return(cmdContext);
        }