Insert data in Migration class

Topics: Writing modules
Apr 2, 2014 at 12:58 PM
Edited Apr 2, 2014 at 1:00 PM
Hello guys!

I want to have a settings with default values in my module and I want to insert this default values in Migration.cs. And I want my settings be a submenu of Settings in Dashboard. I created Driver and Handler and tried to load data in 2 ways.
Like non-content data - I created a class
public class Setting
    {
        public virtual string Format { get; set; }
    }
and Migration
public int Create()
        {
            SchemaBuilder.CreateTable("SettingRecord", t => t.Column<string>("Format"));

            return 1;
        }

        public int UpdateFrom1()
        {
            _repository.Create(new Setting { Format = "###aaaa###" });
            return 2;
        }
And this works well. And I made an inheritance like this:
public interface ISetting
    {
        string Format { get; set; }
    }
public class Setting : ISetting
    {
        public virtual string Format { get; set; }
    }
public class SettingRecord : ContentPartRecord, ISetting
    {
        public virtual string Format { get; set; }
    }
If I use this Setting class inside migration I have the following error:
NHibernate.MappingException: No persister for: Utils.InsertWidgetInContent.Models.Setting

But if I use class without interface everything is ok.

If I create a table and then try to insert data like
var item = _contentManager.New<SomePart>("SomeSettings");
_contentManager.Create(item)
I got an error too, because item is always null.

And questions are:
Is there any way to set default values to my settings?
What is the best way to load default data ? (can I load data inside migration ?)
Why after inheritance NHibernate throws error ?

Thank you very much, guys!
Apr 3, 2014 at 1:53 PM
A call to


_contentManager.New<SomePart>("SomeSettings")


will return null if there is no content type named SomeSettings, or if a content part with type SomePart is not attached to the content type. Check if you have defined the content type and attached the part correctly with a AlterTypeDefinition call in the migration file.

If you want it to be a content part record, you shouldn't use the repository directly to create items. A content item is composed of several parts, and stored in the database in several tables, to keep the version info etc. When you use the repository directly, you create/modify only one table. ContentManager creates all records of the item, including your part.

By the way, since SettingRecord is a content part record, I would recommend complying with naming conventions, and name it SettingPartRecord, and the part class as SettingPart, and SettingPartHandler, SettingPartDriver etc.

Aside from that, you can not use the SettingRecord repository to persist a Setting object. They are not the same. You would use

_repository.Create(new SettingRecord { Format = "###aaaa###" });

instead, but this is not so relevant since you want a content type/content part to show in settings

Apr 7, 2014 at 1:01 PM
Thank you kassobasi!

I did it in that way.
Apr 9, 2014 at 3:10 PM
Hello again!

Now I have another problem with migration.
My migration code:
public int Create()
        {
            SchemaBuilder.CreateTable("SettingPartRecord", t => t.ContentPartRecord().Column<string>("Format"));

            ContentDefinitionManager.AlterPartDefinition("InsertWidgetInContentSettingPart", p => p
                .WithDescription("Used by the InsertWidgetInContent content type."));

            ContentDefinitionManager.AlterTypeDefinition("InsertWidgetInContentSetting",
               cfg => cfg
                   .WithPart("InsertWidgetInContentSettingPart")
                );

            return 1;
        }

public int UpdateFrom1()
        {
            var item = _contentManager.New("InsertWidgetInContentSetting").As<InsertWidgetInContentSettingPart>();
            item.Format = Format;
            _contentManager.Create(item);

            return 2;
        }
It creates a record with Id = 1 and Null format.

I googled a lot, but can't find a solution.

Could you help me ?
Apr 9, 2014 at 9:11 PM
It shouldn't create a record with id 1. Since it's a content part record, the id should be the Id or the content item, which should be more than one since some content items, like the admin user and the welcome page are created at site setup (if this didn't change in 1.8, I can't check right now). Can you check if there are errors logged during the site setup?