Ejemplo n.º 1
0
        public TEntity Create(TEntity entity)
        {
            var properties = _dataMapper.MapToProperties(entity);
            var expression = _expressionProvider.ProvideCreateExpressionWithSetIdentity(_tableName, properties);

            var command = new SqlCommand(expression, _connection)
            {
                Transaction = _transaction
            };

            var idParameter = new SqlParameter
            {
                ParameterName = "@id",
                SqlDbType     = SqlDbType.Int,
                Direction     = ParameterDirection.Output
            };

            command.Parameters.Add(idParameter);
            command.ExecuteNonQuery();

            expression = _expressionProvider.ProvideReadOneExpression(_tableName, idParameter.Value);

            command = new SqlCommand(expression, _connection)
            {
                Transaction = _transaction
            };

            var reader = command.ExecuteReader();

            entity = _dataMapper.MapToEntity(reader);

            reader.Close();

            return(entity);
        }
Ejemplo n.º 2
0
        public TEntity Create(TEntity entity)
        {
            var properties = _dataMapper.MapToProperties(entity);
            var expression = _expressionProvider.ProvideCreateExpressionWithSelectIdentity(_tableName, properties);
            var id         = _connection.Query <int>(expression, entity, _transaction).Single();

            expression = _expressionProvider.ProvideReadOneExpression(_tableName, id);
            entity     = _connection.QueryFirstOrDefault <TEntity>(expression, null, _transaction);

            return(entity);
        }