Migration error: attempted to assign id from null one-to-one property: ContentItemRecord

Topics: General, Writing modules
May 20, 2013 at 8:25 AM
Edited May 20, 2013 at 8:26 AM
Perhaps someone can shed some light on this, I've been looking at it too long. Here is my migration code:
public Migrations(IRepository<ProductPartRecord> productPartRepository, IRepository<CategoryPartRecord> categoryPartRepository)
        {
            _productPartRepository = productPartRepository;
            _categoryPartRepository = categoryPartRepository;
        }

        public int Create() {
            ContentDefinitionManager.AlterTypeDefinition("Category", builder => builder
                .WithPart("CommonPart")
                .WithPart("TitlePart")
                .WithPart("AutoroutePart"));

            ContentDefinitionManager.AlterTypeDefinition("Category", builder => builder
                .WithPart("AutoroutePart", partBuilder => partBuilder
                    .WithSetting("AutorouteSettings.AllowCustomPattern", "true")
                    .WithSetting("AutorouteSettings.AutomaticAdjustmentOnEdit", "false")
                    .WithSetting("AutorouteSettings.PatternDefinitions", "[{Name:'Category Title', Pattern: 'category/{Content.Slug}', Description: 'category/category-title'}]")));
     
            SchemaBuilder.CreateTable("CategoryPartRecord", table => table
                .ContentPartRecord()
                    .Column<string>("Name")
                    .Column<string>("Description")
                    .Column<string>("Image")
                );

            ContentDefinitionManager.AlterTypeDefinition("Category", builder => builder
                .WithPart("CategoryPart"));

            ContentDefinitionManager.AlterTypeDefinition("Category", builder => builder
                .Creatable()
                .Draftable());

            return 1;
        }


        public int UpdateFrom1() {

            _categoryPartRepository.Create(new CategoryPartRecord { Name = "Category1", Description = "Description1", Image = "Image1" });

            return 2;
        }
UpdateFrom1 obviously attempts to insert a dummy record, but this causes nHibernate to throw this exception:

"attempted to assign id from null one-to-one property: ContentItemRecord"

Below is the part record:
    public class CategoryPartRecord : ContentPartRecord {
        public CategoryPartRecord()
        {
            CategoryProducts = new List<CategoryProductRecord>();
        }

        public virtual string Name { get; set; }
        public virtual string Description { get; set; }
        public virtual string Image { get; set; }

        public virtual IList<CategoryProductRecord> CategoryProducts { get; set; }
    }
Am I approaching this incorrectly?
May 21, 2013 at 12:50 AM
I found this older post:

https://orchard.codeplex.com/discussions/256560

Here he uses:
            _showRepository.Create(new Show
            {
                show_nm = "Show Name",
                show_act_ind = true,
                show_img = "image"
            });
...which appears to be exactly what I am doing. Any clues?
May 21, 2013 at 1:00 AM
I am also attempting to link Category to Product (many to many). See below:
   public class ProductPartRecord : ContentPartRecord {
        public ProductPartRecord()
        {
            CategoryProducts = new List<CategoryProductRecord>();
        }
        public virtual string ProductCode { get; set; }
        public virtual string Description { get; set; }
        public virtual string Image { get; set; }
        public virtual string Notes { get; set; }
        public virtual decimal Price { get; set; }

        public virtual IList<CategoryProductRecord> CategoryProducts { get; set; }
    }
and:
    public class CategoryProductRecord
    {
        public virtual int Id { get; set; }
        public virtual CategoryRecord CatergoryRecord { get; set; }
        public virtual ProductRecord ActorRecord { get; set; } 
    }
May 21, 2013 at 7:25 AM
OK, figured it out. I wanted to create a ContentPartRecord, so here is how I did it:
            var item = _contentManager.New("Category").As<CategoryPart>();
            item.Name = "My Name";
            item.Image = "My Image";
            item.Description = "My Description";

            _contentManager.Create(item);
            _contentManager.Publish(item.ContentItem);
May 21, 2013 at 11:45 PM
Actually what I really needed to know was that there's an import / export module which will make light work of everything!