1 to N relationship - null id Error

Topics: Writing modules
Feb 13 at 7:11 PM
Hi All!
I try to develop custom module with 1 to N relationship
Skywalkers WebShop tutorial taken as a basis (part 9)
So...
Models/AdSetRecord.cs
public class AdSetRecord
    {
        public AdSetRecord() {
            AdEntityRecords = new List<AdEntityRecord>();
        }

        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual IList<AdEntityRecord> AdEntityRecords { get; set; }
    }
AdEntityRecord.cs
public class AdEntityRecord
    {
        public virtual int Id { get; set; }
        public virtual int AdSetRecord_Id { get; set; }
        [Required]
        public virtual string Name { get; set; }
        [StringLengthMax]
        [Required]
        public virtual string Code { get; set; }
    }
AdSetPart.cs
public class AdSetPartRecord : ContentPartRecord
    {
        public virtual string Name { get; set; }
        public virtual string Css { get; set; }
        public virtual AdSetRecord AdSetRecord { get; set; }
    }

    public class AdSetPart : ContentPart<AdSetPartRecord> {

        public string Name
        {
            get { return Record.Name; }
            set { Record.Name = value; }
        }

        public string Css
        {
            get { return Record.Css; }
            set { Record.Css = value; }
        }

        public AdSetRecord AdSet
        {
            get { return Record.AdSetRecord; }
            set { Record.AdSetRecord = value; }
        }

    }
Migrations.cs
public class Migrations : DataMigrationImpl
    {
        public int Create() {

            SchemaBuilder.CreateTable("AdSetPartRecord", table => table
                .ContentPartRecord()
                .Column<string>("Name")
                .Column<string>("Css")
                .Column<int>("AdSetRecord_Id"));

            SchemaBuilder.CreateTable("AdSetRecord", table => table
                .Column<int>("Id", column => column.PrimaryKey().Identity())
                .Column<string>("Name"));
                

            SchemaBuilder.CreateTable("AdEntityRecord", table => table
                .Column<int>("Id", column => column.PrimaryKey().Identity())
                .Column<int>("AdSetRecord_Id")
                .Column<string>("Name")
                .Column<string>("Code", column => column.Unlimited()));


            SchemaBuilder.CreateForeignKey("AdEntityRecord_AdSetRecord", "AdEntityRecord", new[] { "AdSetRecord_Id" }, "AdSetRecord", new[] { "Id" });


            ContentDefinitionManager.AlterPartDefinition("AdSetPart", cfg => cfg
                .Attachable());

            ContentDefinitionManager.AlterTypeDefinition("AdSetWidget", cfg => cfg
                .WithPart("AdSetPart")
                .WithPart("WidgetPart")
                .WithPart("CommonPart")
                .WithSetting("Stereotype", "Widget"));

            return 1;
        }

    }
Then, when i try to create AdEntityRecord and save to DB i get following error:
null id in Ti.Ads.Models.AdEntityRecord entry (don't flush the Session after an exception occurs)
What the problem?
Please help!
Feb 13 at 7:12 PM
Stack Trace:
[AssertionFailure: null id in Ti.Ads.Models.AdEntityRecord entry (don't flush the Session after an exception occurs)]
   NHibernate.Event.Default.DefaultFlushEntityEventListener.CheckId(Object obj, IEntityPersister persister, Object id, EntityMode entityMode) in c:\Users\sebros\Documents\My Projects\nhibernate-core\src\NHibernate\Event\Default\DefaultFlushEntityEventListener.cs:117
   NHibernate.Event.Default.DefaultFlushEntityEventListener.GetValues(Object entity, EntityEntry entry, EntityMode entityMode, Boolean mightBeDirty, ISessionImplementor session) in c:\Users\sebros\Documents\My Projects\nhibernate-core\src\NHibernate\Event\Default\DefaultFlushEntityEventListener.cs:84
   NHibernate.Event.Default.DefaultFlushEntityEventListener.OnFlushEntity(FlushEntityEvent event) in c:\Users\sebros\Documents\My Projects\nhibernate-core\src\NHibernate\Event\Default\DefaultFlushEntityEventListener.cs:36
   NHibernate.Event.Default.AbstractFlushingEventListener.FlushEntities(FlushEvent event) in c:\Users\sebros\Documents\My Projects\nhibernate-core\src\NHibernate\Event\Default\AbstractFlushingEventListener.cs:161
   NHibernate.Event.Default.AbstractFlushingEventListener.FlushEverythingToExecutions(FlushEvent event) in c:\Users\sebros\Documents\My Projects\nhibernate-core\src\NHibernate\Event\Default\AbstractFlushingEventListener.cs:61
   NHibernate.Event.Default.DefaultAutoFlushEventListener.OnAutoFlush(AutoFlushEvent event) in c:\Users\sebros\Documents\My Projects\nhibernate-core\src\NHibernate\Event\Default\DefaultAutoFlushEventListener.cs:32
   NHibernate.Impl.SessionImpl.AutoFlushIfRequired(ISet`1 querySpaces) in c:\Users\sebros\Documents\My Projects\nhibernate-core\src\NHibernate\Impl\SessionImpl.cs:1180
   NHibernate.Impl.SessionImpl.List(IQueryExpression queryExpression, QueryParameters queryParameters, IList results) in c:\Users\sebros\Documents\My Projects\nhibernate-core\src\NHibernate\Impl\SessionImpl.cs:649
   NHibernate.Impl.AbstractSessionImpl.List(IQueryExpression queryExpression, QueryParameters parameters) in c:\Users\sebros\Documents\My Projects\nhibernate-core\src\NHibernate\Impl\AbstractSessionImpl.cs:93
   NHibernate.Impl.ExpressionQueryImpl.List() in c:\Users\sebros\Documents\My Projects\nhibernate-core\src\NHibernate\Impl\ExpressionQueryImpl.cs:61
   NHibernate.Linq.DefaultQueryProvider.ExecuteQuery(NhLinqExpression nhLinqExpression, IQuery query, NhLinqExpression nhQuery) in c:\Users\sebros\Documents\My Projects\nhibernate-core\src\NHibernate\Linq\DefaultQueryProvider.cs:103
   NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression) in c:\Users\sebros\Documents\My Projects\nhibernate-core\src\NHibernate\Linq\DefaultQueryProvider.cs:35
   NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression) in c:\Users\sebros\Documents\My Projects\nhibernate-core\src\NHibernate\Linq\DefaultQueryProvider.cs:40
   Remotion.Linq.QueryableBase`1.GetEnumerator() in :0
   System.Linq.Enumerable.ToDictionary(IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer) +173
   System.Linq.Enumerable.ToDictionary(IEnumerable`1 source, Func`2 keySelector) +125
   Orchard.Data.Migration.DataMigrationManager.GetFeaturesThatNeedUpdate() in c:\Projects\Scicentr\src\Orchard\Data\Migration\DataMigrationManager.cs:48
   Orchard.Modules.Data.Migration.<GetNotifications>d__6.MoveNext() +219
   System.Linq.<SelectManyIterator>d__14`2.MoveNext() +507
   System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +536
   System.Linq.Enumerable.ToList(IEnumerable`1 source) +80
   Orchard.UI.Admin.Notification.NotificationManager.GetNotifications() in c:\Projects\Scicentr\src\Orchard\UI\Admin\Notification\NotificationManager.cs:20
   Orchard.UI.Admin.Notification.AdminNotificationFilter.OnResultExecuting(ResultExecutingContext filterContext) in c:\Projects\Scicentr\src\Orchard\UI\Admin\Notification\NotificationFilter.cs:35
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +70
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +799724
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +799724
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +799724
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +799724
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +799724
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +799724
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +263
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +799960
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__19() +40
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +15
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +65
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +51
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +51
   Orchard.Mvc.Routes.HttpAsyncHandler.EndProcessRequest(IAsyncResult result) in c:\Projects\Scicentr\src\Orchard\Mvc\Routes\ShellRoute.cs:159
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288