Using ContentManager in Migrations

Topics: Writing modules
Jul 24, 2013 at 10:59 PM
Edited Jul 24, 2013 at 11:05 PM
I am trying to create new records for the following content parts, but cannot seem to be able to create records using the Content Manager.

I cannot get something like this to work:
 ContentDefinitionManager.AlterTypeDefinition("MyObject", builder =>
                    .WithPart("AutoroutePart", part =>
                        .WithSetting("AutorouteSettings.AllowCustomPattern", "true")
                        .WithSetting("AutorouteSettings.AutomaticAdjustmentOnEdit", "false")
                        .WithSetting("AutorouteSettings.PatternDefinitions", "[{Name:'Winning Numbers Title', Pattern: 'winning-numbers/{Content.Slug}', Description: 'winning-numbers/winning-numbers-title'},{Name:'Winning Numbers Title', Pattern:'winning-numbers/{Content.Slug}', Description:'winning-numbers/winning-numbers-title'}]")
                        .WithSetting("AutorouteSettings.DefaultPatternIndex", "0")

            SchemaBuilder.CreateTable("MyObjectPartRecord", table => table

            ContentDefinitionManager.AlterTypeDefinition("MyObject", builder => builder.WithPart("MyObjectPart"));

            ContentDefinitionManager.AlterPartDefinition("MyObjectPart", builder => builder.Attachable());

            DateTime beginDate = DateTime.Now.AddDays(-90);

            MyObjectPart p = MyObjectBuilder.GetNumber(DateTime.Now);

            _contentManager.Create(p.ContentItem, VersionOptions.Published);
Jul 25, 2013 at 6:16 AM
How does it not work? Please give details about what you are trying to do, what you tried, and how it failed.
Jul 25, 2013 at 1:09 PM
Edited Jul 25, 2013 at 1:10 PM
I have tried it so many different ways, each not working. I think this particular way one of the p properties comes back as null.

It is only the ContentManager I have issues w/.
Jul 25, 2013 at 6:32 PM
Try to do your content manager manipulations in a separate migration step. Creating content items from a migration can be problematic, in particular using types that you just created from the same step. Migrations are not really designed for content creation, but more for schema changes. Alternatively, you may want to move this to a recipe, if that makes sense.
Jul 26, 2013 at 3:27 PM
I am following you on what you say. It makes sense.

I am trying to do what you suggest and I am getting a null exception on the following:
var record = _contentManager.New("MyObjectPart").As<MyObjectPart>();
record.Date = DateTime.Now;
record is coming back as null. I am not sure what else needs to be done to instantiate the record object.

Jul 26, 2013 at 8:31 PM
The argument for New is the content type name, not the part name.