Not added content field to custom content type programmatically

Topics: Customizing Orchard
Sep 19 at 7:30 AM
Create new content type via migration, added to him content field, but they not displayed in dashboard. Content type view in Dashboard What wrong with my code?
    public int UpdateFrom2() {

            var name = "ProductViaCode";
            ContentDefinitionManager.AlterPartDefinition(
                string.Format("{0}Part", name),
                    b => b
                        .Attachable()
                        .WithField("ProductId", cfg => cfg
                            .OfType("InputField")
                            .WithDisplayName("Product Id")));

            ContentDefinitionManager.AlterTypeDefinition(
                name, cfg => cfg
                .WithPart(typeof(CommonPart).Name)
                .WithPart(typeof(AutoroutePart).Name)
                .WithPart(typeof(BodyPart).Name)
                .WithPart(typeof(TitlePart).Name)
                .WithPart(typeof(MenuPart).Name)
                .Creatable()
                .Draftable()
                .Listable()
                .Securable());

            return 3;
        }
Developer
Sep 19 at 9:27 AM
Edited Sep 19 at 9:29 AM
In order to attach a content field to a content type, you need to create an "implicit" content part that has the exact same name as the content type, without the "Part" suffix. Using your example, this means the name of the part you're creating needs to be "ProductViaCode".

Next, make sure to actually attach that implicit part to your content type. The following code should work:
public int UpdateFrom2() {

            var name = "ProductViaCode";
            ContentDefinitionManager.AlterPartDefinition(
                name, // Use the name of the content type without the "Part" suffix.
                    b => b
                        // .Attachable() -> There is no need to make this part attachable, since it is an "implicit" part for the "ProductViaCode" content type.
                        .WithField("ProductId", cfg => cfg
                            .OfType("InputField")
                            .WithDisplayName("Product Id")));

            ContentDefinitionManager.AlterTypeDefinition(
                name, cfg => cfg
                .WithPart(typeof(CommonPart).Name)
                .WithPart(typeof(AutoroutePart).Name)
                .WithPart(typeof(BodyPart).Name)
                .WithPart(typeof(TitlePart).Name)
                .WithPart(typeof(MenuPart).Name)
                .WithPart(name) // Add the implicit part to the content type.
                .Creatable()
                .Draftable()
                .Listable()
                .Securable());

            return 3;
        }
Marked as answer by JeKachan on 9/19/2016 at 4:17 AM
Sep 19 at 12:17 PM
Thanks a lot! :)