---
title: "Create a custom model"
slug: "create-a-custom-model"
abstract: "How to create custom facets and events, a custom model, and how to deploy the model."
---
# Create a custom model

This topic describes how to define a custom model.

<details>
<summary><h2>Define custom facets and events</h2></summary>

To create custom facets and events:

1. Create a new project - for example, `Documentation.Model`.
2. Define facets and events.

    > [!note]
    > Ensure that all properties adhere to xConnect’ type restrictions. Decorate any facet used in the tracker with the `[Serializable]` attribute.

    ```csharp
    using Sitecore.XConnect;
    using System;

    namespace Documentation.Model
    {
        [Serializable]
        [FacetKey(DefaultFacetKey)]
        public class FrequentFlyerInfo : Sitecore.XConnect.Facet
        {
            public const string DefaultFacetKey = "FrequentFlyerInfo";

            public FrequentFlyerInfo(string frequentFlyerId)
            {
                FrequentFlyerID = frequentFlyerId;
            }

            public FrequentFlyerStatus Status { get; set; }
            public string FrequentFlyerID { get; set; } // Example: ABC12345

            public enum FrequentFlyerStatus
            {
                Unknown = 0,
                Bronze,
                Silver,
                Gold
            }
        }

        public class BoardFlight : Event
        {
            public static readonly Guid EventDefinitionId = new Guid("2A72E131-BCFD-481C-8E15-04496E9586DC");

            public BoardFlight(DateTime timestamp) : base(EventDefinitionId, timestamp)
            {

            }

            public string Seat { get; set; }
            public bool Upgraded { get; set; }
        }
    }
    ```

</details>

<details>
<summary><h2>Create a model</h2></summary>

To create a model:

1. Create a class named `CollectionModel` - for example, `Documentation.Model.CollectionModel`. Create an instance of the `XdbModelBuilder` as shown:

    ```csharp
    using Sitecore.XConnect;
    using Sitecore.XConnect.Schema;
    using System;

    namespace Documentation.Model
    {
        public class CollectionModel
        {
            public static XdbModel Model { get; } = BuildModel();

            private static XdbModel BuildModel()
            {
                XdbModelBuilder modelBuilder = new XdbModelBuilder("DocumentationModel", new XdbModelVersion(1, 0));

                // Facets and events here

                return modelBuilder.BuildModel();
            }
        }
    }
    ```
    > [!note]
    > The model’s full name (represented by the `FullName` property of the `XdbModel` class) includes the model’s name and version. For example: `DocumentationModel,1.0`.

2. Optionally, reference another model that you want to use. In the following example, the `Sitecore.XConnect.Collection.Model.CollectionModel.Model` is referenced. Classes such as `Facet` and `Event` are included automatically.

    ```csharp
    using Sitecore.XConnect;
    using Sitecore.XConnect.Schema;
    using System;

    namespace Documentation.Model
    {
        public class CollectionModel
        {
            public static XdbModel Model { get; } = BuildModel();

            private static XdbModel BuildModel()
            {
                XdbModelBuilder modelBuilder = new XdbModelBuilder("DocumentationModel", new XdbModelVersion(1, 0));

                modelBuilder.ReferenceModel(Sitecore.XConnect.Collection.Model.CollectionModel.Model);
                // Facets and events here

                return modelBuilder.BuildModel();
            }
        }
    }
    ```

3. Define the `FrequentFlyerInfo` contact facet and the `BoardFlight` event:

    ```csharp
    using Sitecore.XConnect;
    using Sitecore.XConnect.Schema;
    using System;

    namespace Documentation
    {
        public class CollectionModel
        {
            public static XdbModel Model { get; } = BuildModel();

            private static XdbModel BuildModel()
            {
                XdbModelBuilder modelBuilder = new XdbModelBuilder("DocumentationModel", new XdbModelVersion(1, 0));

                modelBuilder.ReferenceModel(Sitecore.XConnect.Collection.Model.CollectionModel.Model);

                modelBuilder.DefineFacet<Contact, FrequentFlyerInfo>(FrequentFlyerInfo.DefaultFacetKey);
                modelBuilder.DefineEventType<BoardFlight>(false);

                return modelBuilder.BuildModel();
            }
        }
    }
    ```

4. If facets, events, or types inherit a type that is located in a different assembly, use the `modelbuilder.RegisterType()` method to manually register a type. For example, `Documentation.Model.FrequentFlyerInfo` might inherit `Common.Model.InheritedBase`.

    ```csharp
    using Sitecore.XConnect;
    using Sitecore.XConnect.Schema;
    using System;

    namespace Documentation
    {
        public class CollectionModel
        {
            public static XdbModel Model { get; } = BuildModel();

            private static XdbModel BuildModel()
            {
                XdbModelBuilder modelBuilder = new XdbModelBuilder("DocumentationModel", new XdbModelVersion(1, 0));

                modelBuilder.ReferenceModel(Sitecore.XConnect.Collection.Model.CollectionModel.Model);

                modelBuilder.RegisterType<Common.Model.InheritedBase>(false);

                modelBuilder.DefineFacet<Contact, FrequentFlyerInfo>(FrequentFlyerInfo.DefaultFacetKey);
                modelBuilder.DefineEventType<BoardFlight>(false);

                return modelBuilder.BuildModel();
            }
        }
    }
    ```

> [!note]
> In order to use your model you must [deploy your custom model](https://doc.sitecore.com/xp/en/developers/105/sitecore-experience-platform/deploy-a-custom-model.html "Deploy a custom model") to xConnect, the Marketing Automation Engine, and all core Sitecore instances.

</details>