How do I programmatically create an entity with its fields? I'm using Entity API to work with some entities but I cannot figure out how to create a new entity with its fields attached.  I created a bundle and added some fields to that bundle.  I'm looking to do this:

$a = entity_create(...);


return an array:

isNew =&gt; TRUE,
title =&gt; '',
attachedField1 =&gt; '',
attachedField2 =&gt; '',
etc...


I'd then like to be able to simply update the values and then entity_save() them: 

isNew =&gt; TRUE,
title =&gt; 'Title of my entity',
attachedField1 =&gt; 'data for attached field 1',
attachedField2 =&gt; 'data for attached field 2',
etc...


Is this doable?  If so, how?  I'm having a tough time working with entities.

Here's my entity info:

  $return['k_profile'] = array(
      'label' =&gt; t('Profile'),
      'entity class' =&gt; 'Entity',
      'controller class' =&gt; 'EntityAPIController',
      'base table' =&gt; 'k_profile',
      'fieldable' =&gt; TRUE,
      'entity keys' =&gt; array(
        'id' =&gt; 'pid',
        'bundle' =&gt; 'type',
        ),
      'bundles' =&gt; array(), //added later in hook_entity_info_alter()
      'bundle keys' =&gt; array(
        'bundle' =&gt; 'type',
        ),
      'label callback' =&gt; 'entity_class_label',
      'uri callback' =&gt; 'entity_class_uri',
      'access callback' =&gt; TRUE,
      'module' =&gt; 'k_profile',
    );
Here's some code that creates a product programmatically, including fields and properties.

/**
 * Create a product programmatically.
 *
 * This is stolen shamelessly from commerce_bpc. However, I'm not comfortable
 * with the field saving using form api. Seems quite odd.
 *
 * @param $product_type
 *   (string) The name of the product type for which products should be created.
 * @param $values
 *   Keyed array with
 *   - 'price' =&gt; actual amount owed on this installment; decimal text like '1.50'
 *   - 'amount_paid' =&gt; price amount already paid as a decimal text like '1.50';
 *   - 'original_order' =&gt; order id of the original order
 *   - 'original_line_item' =&gt; line item id of original line item
 *   - 'original_product =&gt; product id of the original product from which the
 *     new product is being created.
 * @param $extras
 *   An array for the values of  'extra fields' defined for the product type
 *   entity, or patterns for these. Recognized keys are:
 *   - status
 *   - uid
 *   - sku
 *   - title
 *   Note that the values do NOT come in the form of complex arrays (as they
 *   are not translatable, and can only have single values).
 * @return
 *   The ID of the created product.
 */
function commerce_installments_create_product($product_type, $values, $extras) {
  $form_state = array();
  $form_state['values'] = $values;
  $form = array();
  $form['#parents'] = array();

  // Generate a new product object
  $new_product = commerce_product_new($product_type);

  $new_product-&gt;status = $extras['status'];
  $new_product-&gt;uid = $extras['uid'];

  $new_product-&gt;sku = $extras['sku'];
  $new_product-&gt;title = $extras['title'];
  $new_product-&gt;created = $new_product-&gt;changed = time();

  // field_original_order[und][0][target_id]
  $order = array(LANGUAGE_NONE =&gt; array(0 =&gt; array('target_id' =&gt; $values['original_order'])));
  $form_state['values']['field_original_order'] = $order;

  // field_original_line_item[und][0][target_id]
  $line_item = array(LANGUAGE_NONE =&gt; array(0 =&gt; array('target_id' =&gt; $values['original_line_item'])));
  $form_state['values']['field_original_line_item'] = $line_item;

  $product = array(LANGUAGE_NONE =&gt; array(0 =&gt; array('target_id' =&gt; $values['original_product'])));
  $form_state['values']['field_original_product'] = $product;

  //commerce_price[und][0][amount]
  $price = array(LANGUAGE_NONE =&gt; array(0 =&gt; array(
    'amount' =&gt; $values['price'],
    'currency_code' =&gt; commerce_default_currency(),
  )));
  $form_state['values']['commerce_price'] = $price;

  // field_due_date[und][0][value][date]
  $due_date = array(LANGUAGE_NONE =&gt; array(0 =&gt; array('date' =&gt; $values['due_date'])));
  $form_state['values']['field_due_date'] = $due_date;

  // Notify field widgets to save their field data
  field_attach_submit('commerce_product', $new_product, $form, $form_state);

  commerce_product_save($new_product);
  return $new_product-&gt;product_id;
}