Is there a problem with this tutorial?

Topics: Troubleshooting, Writing modules
Aug 28, 2013 at 4:38 AM
Edited Aug 28, 2013 at 4:38 AM

I've tried this several times, but get the same results each time.

Once i finish the module and go create a product it yellow screens on save with

__null id in Orchard.ContentManagement.Records.ContentTypeRecord entry (don't flush the Session after an exception occurs)
Now I assume it's because on the product entry view, there is no field for the id field in the table as models based on ContentPartRecord has an Id field. But I am confused about that point.

Anyway, how do I get the Id field to be populated?

Best regards,

Aug 28, 2013 at 4:51 AM
You don't have to, it's automatic. The call to ContentPartRecord() in the migration should have ensured that.
Aug 28, 2013 at 12:02 PM
What else would cause the error?
Aug 28, 2013 at 5:44 PM
I don't know. What does the table definition look like?
Aug 28, 2013 at 8:41 PM
It has an Id field created by the ContentPartRecord you mentioned.

It also has a Sku field, and a Price field s described in the tutorial.
Aug 29, 2013 at 1:22 AM
Having an id field is not enough. What are the properties of that column? Is it an identity column, with a seed and an auto-increment?
Aug 29, 2013 at 3:37 AM
I'm not sure what you mean by "having an id field is not enough". It's what the framework created - i didn't make the table through any other means than described in the tutorial.

If I script the table here is the the result.

CREATE TABLE [dbo].[cf_SimpleCommerce_ProductPartRecord](
[Id] [int] NOT NULL,
[Sku] [nvarchar](255) NULL,
[Price] [real] NULL,
[Id] ASC

If having an id field is not enough - what else should have the framework created? Do I need to modify the table with code or script beyond what this?

Here's the create from the migration.
    public int Create() {
        // Creating table ProductPartRecord
        SchemaBuilder.CreateTable("ProductPartRecord", table => table
            .Column("Sku", DbType.String)
            .Column("Price", DbType.Single)

        return 1;
Since you stated that, "You don't have to, it's automatic. The call to ContentPartRecord() in the migration should have ensured that," in answer to my question, "how do I populate the Id field" I don't see the connection to whether or not this is or should be an identity field.

Does the ContentPartRecord() method make the Id field an identity field, or should it? I looked in tables for other modules and it appears not. But I could definitely be wrong on that.
Aug 29, 2013 at 4:35 AM
Edited Aug 29, 2013 at 4:36 AM
Yes, the ContentPartRecord() method sets up the Id column to be an identity and the primary key, so you don't have to (and shouldn't) do so.
The error in your initial post usually does not reflect what's actually going on. Attach a debugger (make sure it breaks on CLR exceptions) and reproduce the error. When the debugger breaks on the exception being thrown, the "Details" link button in the Exception window will reveal what's really going on and give you a clue as to what should be fixed.
Aug 29, 2013 at 4:56 AM
Just trying to help. Everything should have worked as expected, and it didn't, so I'm trying to narrow down on what exactly went wrong. It looks like I was right and for some reason Id didn't end up being an identity. What I'm suspecting happened, but I could be wrong, is that you didn't write the migration exactly right the first time, and modified it afterwards. Unfortunately, it had already run, so didn't run again and any correction was useless. Can you try to zap the database and re-setup the site, to see if it makes a difference in the definition of the Id column? (of course, backup first if you have done anything in the DB that you want to keep.
Aug 29, 2013 at 12:04 PM
I appreciate your help. I have indeed reset / wiped out the Orchard install and redone this a few times. It is very possible that I fouled up the Migration, just not likely. (It is likely i fouled something up tho)

What I will do is restart a clean web site and then download the complete example code and use that module and see what happens.

I'm sure it will work, but I think a confirmation is in order. If it does, I'll then reset and go through the tutorial again. If it still errors out, I'll find out where the code is actually erroring.
Aug 29, 2013 at 12:05 PM
By the way, are you familiar with the pluralsight training on orchad? if so, what is you opinions?
Aug 29, 2013 at 6:43 PM
Thanks, it's perfectly possible something has gone stale in the tutorial. The Pluralsight training is great, I recommend it.
Sep 26, 2013 at 11:12 AM
I faced the same issue, and I found out that I created a table with a wrong name in the migration file.

the created table was named Map while I should have name it MapRecord

i.e.: the wrong code was
            SchemaBuilder.CreateTable("Map", table => table
                .Column("Latitude", DbType.Double)
                .Column("Longitude", DbType.Double)
the correct code
            SchemaBuilder.CreateTable("MapRecord", table => table
                .Column("Latitude", DbType.Double)
                .Column("Longitude", DbType.Double)
Feb 26, 2014 at 7:45 AM
I thought that ContentPart shouldn't have an Identity, but his Id is the same as ContentItem Id which is based on ContentType (which contains our ContentPart). And considering that each ContentType can have only one instance of ContentPart 1:1 relationship is satisfied and ContentPart Id column is regular int, while ContentType Id is Identity (well ContentItems Id, but trying not to confuse people here too much, the namings are already confusing themselves).

Both Betrand and Skywalker, you guys are saying that Id in ContentPart table should be an Identity?

"Yes, the ContentPartRecord() method sets up the Id column to be an identity and the primary key, so you don't have to (and shouldn't) do so."
"It looks like I was right and for some reason Id didn't end up being an identity."

Please help, as I have the same issue, and .ContentPartRecord() was initialized correctly (otherwise I wouldn't even have an Id column I assume).
Feb 26, 2014 at 8:44 AM
My mistake. A content part record table should have an Id set as the primary key, but not as an identity:
public CreateTableCommand ContentPartRecord() {
    Column<int>("Id", column => column.PrimaryKey().NotNull());

    return this;
So in your migrations, you need to make a call to ContentPartRecord() on the content part record table being created.
Feb 26, 2014 at 9:09 AM
Thank you for the swift response.

I have done all that correctly. But still get the same error.
My scenario:
SchemaBuilder.CreateTable("SpecificationRecord", table => table 

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




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


                                    builder => builder.Attachable());

 ContentDefinitionManager.AlterTypeDefinition("ProductItem", builder =>
public class ProductPartRecord : ContentPartRecord
        public ProductPartRecord()
            Specifications = new List<SpecificationRecord>();

        public virtual string Name{ get; set; }

 public virtual IEnumerable<SpecificationRecord> Specifications { get; set; }
public class SpecificationRecord
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual string Value { get; set; }
        public virtual ProductPartRecord ProductRecord { get; set; }
What I find interesting is that I get error "null id in Orchard.ContentManagement.Records.ContentTypeRecord entry" in Localization/Services/DefaultCultureManager.cs ...
Feb 26, 2014 at 3:24 PM
I've put the module in a clean new solution and this is the error I have now:

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

Previously it was something about datetime conversion, but then I've just removed date time fields to see if this will even work.