Error when add Handler

Topics: Customizing Orchard, Writing modules
Oct 18, 2013 at 8:38 PM
Edited Oct 22, 2013 at 6:04 PM
Hi guys,
I developed new module, when I add below code for handler and want to create one content from my new contentType in dashboard and submit form, I get an error. When I remove Handler or comment that the error was fixed but none part of data saved in database.

Handler Code:
public class MyModuleHandler : ContentHandler
    {
        public MyModuleHandler(IRepository<MyModulePartRecord> MyModulePartRepository)
        {
            Filters.Add(StorageFilter.For(MyModulePartRepository));
        }
    }
Error:
null id in Orchard.ContentManagement.Records.ContentItemRecord entry (don't flush the Session after an exception occurs)

I also have below code in migration:
SchemaBuilder.CreateTable("MyModulePartRecord", table => table
                                        .ContentPartRecord()
                                        .Column<string>("Name")
                                        .Column<string>("ImdbRate"));
please guide me.
Excuse me for my bad English.
Thanks...
Oct 20, 2013 at 5:41 AM
Help Please )-:
Oct 20, 2013 at 7:08 AM
Can you post the code from the model classes, record and part? Can you also check if the migration run OK, that is, do you have the table in the orchard DB named <YourModuleName>_SeriesPartRecord?


Oct 22, 2013 at 6:02 PM
Hi,
I checked, migration run OK and table created successfully. here is my model, record and part codes. Thanks...

// MyModulePart.cs
using System;
using Orchard.ContentManagement;

namespace www.MyModule.com.Models {
    public class MyModule : ContentPart<MyModuleRecord> {
        public string Name { get { return Record.Name; } set { Record.Name = value; } }
        public string ImdbRate { get { return Record.ImdbRate; } set { Record.ImdbRate = value; } } 
    }
}
// MyModulePartRecord.cs
using System;
using Orchard.ContentManagement.Records;

namespace www.MyModule.com.Models
{
    public class MyModulePartRecord : ContentPartRecord
    {
        public virtual string Name { get; set; }
        public virtual string ImdbRate { get; set; }
    }
}
// Migration Code
SchemaBuilder.CreateTable("MyModulePartRecord", table => table
                                        .ContentPartRecord()
                                        .Column<string>("Name")
                                        .Column<string>("ImdbRate"));
// Handler Code
using Orchard.ContentManagement.Handlers;
using Orchard.Data;
using www.MyModule.com.Models;

namespace www.MyModule.com.Handlers {
    public class MyModuleHandler : ContentHandler
    {
        public MyModuleHandler(IRepository<MyModulePartRecord> MyModulePartRepository)
        {
            Filters.Add(StorageFilter.For(MyModulePartRepository));
        }
    }
}
Oct 22, 2013 at 11:03 PM
I guess you are changing the names of classes in the code before posting here, but can you make sure they are consistent in your code? Here the same class is referred to as MyModuleRecord and MyModulePartRecord. Maybe you both in the Models folder. Also, I think it's just a convention but, the part class name normally gets a Part suffix, so the names of classes would be MyModulePart an MyModulePartRecord.

Other than that code looks ok. You might want to start over with a clean setup, and a new DB to make sure the latest version still causes an exception. If you are developing with SQL CE just delete the app_data folder in Orchard.web and restart. You might prefer just deleting nhibernate mappings first.

You can also try attaching a debugger and find where the original exception (one probably swallowed by orchard code) happens.

Good luck.