Creating a part with you own ID column

Topics: General, Writing modules
Aug 23, 2012 at 4:57 PM

I know when I normally create a tablepreviously in the Migration file that i use the

.ContentPartRecord() and this creates the Id column

But I have removed this and amended one of my columns to

.Column<string>("ItemNo", column => column.PrimaryKey().WithLength(20))

But when I an an item it never saves, have I missed something - it worked fine before creating an Id itself?

Thanks

Aug 23, 2012 at 6:56 PM

Might need to see the code where you create the record to be saved. 

Aug 23, 2012 at 7:50 PM

That's because Id was an identity column. The column was created something like this:

.Column<int>("Id", column => column.PrimaryKey().Identity())

You don't set the value. It will be created on its own.

In your case, you need to set a value for ItemNo before saving it to the database. If you don't, it will be null.

The documentation is pretty good on this subject: http://docs.orchardproject.net/Documentation/Understanding-data-access

 

Regards,

Dave

Aug 29, 2012 at 12:01 PM
I did set a value though, or so I thought.
The code that works fine is as follows, Migration.cs
        public int UpdateFrom2()
        {

            SchemaBuilder.CreateTable("DynamicsNavItemPartRecord", table => table
                .ContentPartRecord()
                //.Column<string>("ItemNo", column => column.PrimaryKey().Unique().Identity().WithLength(20))
                .Column<string>("ItemNo", column => column.WithLength(20).Unique())
                .Column<string>("Description", column => column.WithLength(100))
                .Column<string>("Size", column => column.WithLength(50))
                .Column<string>("Colour", column => column.WithLength(50))
                .Column<string>("UoM", column => column.WithLength(20))
                .Column<int>("PurchaseUoM")
                .Column<int>("Qty")
                .Column<string>("Brand", column => column.WithLength(20))
                .Column<string>("Gender", column => column.WithLength(20))
                .Column<bool>("Published")
            );

            ContentDefinitionManager.AlterPartDefinition("DynamicsNavItemPart", part => part
                .Attachable()
                );



            return 3;
        }

 

And my code to create an item is:

// create content type!
var item = _contentManager.New<DynamicsNavItemPart>("Item");
item.ItemNo = i.No;
item.Description = i.Description + i.Description2;
item.Colour = i.Colour;
item.Size = i.Size;
item.UoM = i.BaseUoM;
item.PurchaseUoM = 1;
item.Brand = i.Brand;
item.Gender = i.Gender;
item.Qty = decimal.Parse(i.AvailableQty);
item.Published = true;

// set content item title
var title = item.ContentItem.Get<TitlePart>();
title.Title = item.Description;

// Create content item
_contentManager.Create(item);

 All this works fine. After changing a few things, I removed everything from App_Data to start a fresh, Migration.cs now looks like

        public int UpdateFrom2()
        {

            SchemaBuilder.CreateTable("DynamicsNavItemPartRecord", table => table
                //.ContentPartRecord()
                .Column<string>("ItemNo", column => column.PrimaryKey().WithLength(20))
                //.Column<string>("ItemNo", column => column.WithLength(20).Unique())
                .Column<string>("Description", column => column.WithLength(100))
                .Column<string>("Size", column => column.WithLength(50))
                .Column<string>("Colour", column => column.WithLength(50))
                .Column<string>("UoM", column => column.WithLength(20))
                .Column<int>("PurchaseUoM")
                .Column<int>("Qty")
                .Column<string>("Brand", column => column.WithLength(20))
                .Column<string>("Gender", column => column.WithLength(20))
                .Column<bool>("Published")
            );

            ContentDefinitionManager.AlterPartDefinition("DynamicsNavItemPart", part => part
                .Attachable()
                );



            return 3;
        }

My code stayed the same, with me removing the ContentPartRecord and setting the ItemNo field to a primary key I thought it should work, As you can see ItemNo is being set but nothing is saved.

Have I missed something?

Thanks

 

Developer
Aug 29, 2012 at 1:08 PM

I think that your table is now missing an Id field. I believe that Content Parts are linked to their content items based on an Id column.

Aug 29, 2012 at 2:57 PM

Didn't realise that, but thinking of it now makes sense!

Thanks