# Product Definitions & Fields

Creating Product Definitions and their related definition fields is the most frequent use for definitions. The taxonomy of products differs from implementation to implementation, and below are a few examples of how they can be created via code.

{% hint style="info" %}
The code in these examples can and should be customized for individual use cases.
{% endhint %}

## Product Definition Fields

Start by creating as many Product Definition Fields as needed for your Product Definition. We start with the fields because when creating the definition, we can then immediately add the fields before we save anything to the database.

{% code overflow="wrap" %}

```csharp
private ProductDefinitionFieldEntity CreateProductDefinitionField(
    DataTypeEntity dataType,
    string name,
    bool isMultilingual,
    bool isVariantProperty)
{
    return new ProductDefinitionFieldEntity
    {
        Name = name,
        Deleted = false,
        Multilingual = isMultilingual,
        DisplayOnSite = true,
        RenderInEditor = true,
        IsVariantProperty = isVariantProperty,
        DataType = dataType
    };
}
```

{% endcode %}

The method above can be re-used to create multiple Product Definition Fields by supplying:

* dataType - denoting the field should be displayed as, for example, a short text or a number field.
* name - the name of the field that will appear on the product editor.
* isMultilingual - if true, the field will be able to have differentiated values for each configured language.
* isVariantProperty - if true, the field will be available only on variants; otherwise, it will be available only on products.

## Product Definition

Continue with creating a Product Definition.

{% code overflow="wrap" %}

```csharp
private ProductDefinitionEntity CreateProductDefinition(
string name, 
string description)
{
    return new ProductDefinitionEntity
    {
        Name = name,
        Description = description,
        Deleted = false
    };
}
```

{% endcode %}

* name - Name of the definition, denoting the type of product.
* description - A description of what the definition is for.

## Create & Save

The below example ties it all together. This is done within the context of a HostedService, which is covered in [Bootstrapping data on startup](/readme/how-to/entities-from-code/bootstrapping-data-on-startup.md).

{% code overflow="wrap" %}

```csharp
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        await using var asyncScope = _serviceProvider.CreateAsyncScope();
        var dbContext = asyncScope.ServiceProvider.GetRequiredService<UcommerceDbContext>();
        
        var exists = dbContext.Set<ProductDefinitionEntity>()
            .Any(x => x.Name == "Shirt");
        
        if (exists)
        {
            return;
        }
        
        var shortTextDataType = dbContext
            .Set<DataTypeEntity>()
            .First(x => x.DefinitionName == "ShortText");
        
        var shirtDefinition = CreateProductDefinition("Shirt", "Definition for Shirt type products.");
        
        shirtDefinition.ProductDefinitionFields = new List<ProductDefinitionFieldEntity>()
        {
            CreateProductDefinitionField(shortTextDataType, "Color", true, true),
            CreateProductDefinitionField(shortTextDataType, "Code", false, false)
        };
        
        dbContext.Add(shirtDefinition);
        
        await dbContext.SaveChangesAsync(stoppingToken);
    }
```

{% endcode %}

{% hint style="info" %}
Note the early return if the Definition already exists. Since this code runs on each startup, there must be a check so it only gets executed when necessary. In this case, if the definition already exists.
{% endhint %}

* Create a scope within which to resolve a UcommerceDbContext.&#x20;
* Implement a check to decide if the bootstrapping should run on the database.&#x20;
* Find the right data type (in this example, we use ShortText for both fields).
* Create the Product Definition using our helper method.
* Create and add Product Definition Fields to the definition (in this example, we create two, one multilingual variant property for color and one non-multilingual product property for barcode)
* Add the definition to the context and save changes to the database.

Once the application starts, the new definition will be selectable for products in the backoffice.

## Related Articles

{% content-ref url="/pages/4F8D6TkbAjEn0G20TAYf" %}
[Bootstrapping data on startup](/readme/how-to/entities-from-code/bootstrapping-data-on-startup.md)
{% endcontent-ref %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dev.ucommerce.net/readme/how-to/entities-from-code/product-definitions-and-fields.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
