Programmatically creating a new instance of a custom content entity that contains a field collection I am trying to write a script that will create about a thousand instances of a custom content entity type I defined via a module from existing data that I have prepared. Right now I just need to figure out how to create an instance of my entity type programmatically.

This entity type contains multiple field collections. I don't understand how to structure the arguments of {entity_type}::create() to accommodate field collections.

Following the accepted answers to Programmatically create nodes and How to programmatically insert and update Field Collection values?, here is my function code so far (i have it inside hook_entity_view_alter() just for easy testing):

$food = Food::create([
    'type'  =&gt; 'food',
    'title' =&gt; 'Allspice',
    'field_category' =&gt; 'Spices'
]);
$variation = FieldCollectionItem::create([
    'field_ingredients' =&gt; 'allspice',
    'field_safe_for' =&gt; [
        'field_nut_allergies' =&gt; 'Yes',
        'field_seafood_allergies' =&gt; 'Yes',
    ]
]);
$variation-&gt;setHostEntity($food);
$food-&gt;field_variations[] = ['field_collection_item' =&gt; $variation];
$food-&gt;save();


where food is my entity type, field_variations is the ID of the field collection in food which may have unlimited values per food instance, and field_safe_for is a nested field collection.

No matter what I do, I always get this:

The website encountered an unexpected error. Please try again later.

Drupal\Core\Entity\EntityStorageException: Missing bundle for entity type field_collection_item in Drupal\Core\Entity\ContentEntityStorageBase-&gt;doCreate() (line 83 of core\lib\Drupal\Core\Entity\ContentEntityStorageBase.php).
Drupal\Core\Entity\EntityStorageBase-&gt;create(Array) (Line: 509)
Drupal\Core\Entity\Entity::create(Array) (Line: 39)
food_food_view_alter(Array, Object, Object) (Line: 501)
Drupal\Core\Extension\ModuleHandler-&gt;alter(Array, Array, Object, Object) (Line: 268)
Drupal\Core\Entity\EntityViewBuilder-&gt;buildMultiple(Array) (Line: 203)
Drupal\Core\Entity\EntityViewBuilder-&gt;build(Array)
call_user_func(Array, Array) (Line: 381)
Drupal\Core\Render\Renderer-&gt;doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer-&gt;render(Array, ) (Line: 226)
Drupal\Core\Render\MainContent\HtmlRenderer-&gt;Drupal\Core\Render\MainContent\{closure}() (Line: 574)
Drupal\Core\Render\Renderer-&gt;executeInRenderContext(Object, Object) (Line: 227)
Drupal\Core\Render\MainContent\HtmlRenderer-&gt;prepare(Array, Object, Object) (Line: 117)
Drupal\Core\Render\MainContent\HtmlRenderer-&gt;renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber-&gt;onViewRenderArray(Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher-&gt;dispatch('kernel.view', Object) (Line: 144)
Symfony\Component\HttpKernel\HttpKernel-&gt;handleRaw(Object, 1) (Line: 62)
Symfony\Component\HttpKernel\HttpKernel-&gt;handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session-&gt;handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle-&gt;handle(Object, 1, 1) (Line: 98)
Drupal\page_cache\StackMiddleware\PageCache-&gt;pass(Object, 1, 1) (Line: 77)
Drupal\page_cache\StackMiddleware\PageCache-&gt;handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware-&gt;handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware-&gt;handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel-&gt;handle(Object, 1, 1) (Line: 628)
Drupal\Core\DrupalKernel-&gt;handle(Object) (Line: 19)


which I do not understand in the slightest since my custom entity types don't make use of bundles.

How can I fix this?
The bundle of the entity type "field_collection_item" is field_name and the error messages says this is missing:

$variation = FieldCollectionItem::create([
  'field_name' =&gt; 'my_field_name',
  ...