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

Topics: General
Jul 26, 2011 at 6:43 AM

I have been getting this error doing something I would think should be extremely simply.  There are just no good tutorials for creating a module from the ground up that actually uses the database with content parts with explanations.  I have been copying the code from existing modules as best as I could, but am still getting the error in the title.  I am trying to create a simple feature content slider.  I need to have a list of the feature sliders, then each sliders has a list of media associated with it, etc.  At this point I am just trying to get to the point where I can save a content slider (name, autoplay)  to the database.  Here is the code that I think is important.

Migrations.cs

public int Create() {
			SchemaBuilder.CreateTable("FeatureSliderItemSettingsRecord", table => table
				.Column("Id", DbType.Int32, column => column.PrimaryKey().Identity())
				.Column("Name", DbType.String)
				.Column("Caption", DbType.String)
				.Column("Title", DbType.String)
				.Column("Position", DbType.Int32)
			);

			SchemaBuilder.CreateTable("FeatureSliderPartRecord", table => table
				.ContentPartRecord()
				.Column("AutoPlay", DbType.Boolean)
				.Column("FeatureSliderName", DbType.String)
				.Column("SelectedPlugin", DbType.Byte)
			);
			SchemaBuilder.CreateTable("FeatureSliderSettingsRecord", table => table
				.Column("Id", DbType.Int32, column => column.PrimaryKey().Identity())
				.Column("FeatureItemGalleryName", DbType.String)
				.Column("KeepAspectRatio", DbType.Boolean)
			);

            ContentDefinitionManager.AlterTypeDefinition("Slider",
                 cfg => cfg
                     .WithPart("FeatureSliderPart")
                     .WithPart("CommonPart")
                 );

            ContentDefinitionManager.AlterPartDefinition("FeatureSliderPart", builder => builder.Attachable());

            return 1;

 

AdminController.cs

[HttpPost, ActionName("Create")]
        public ActionResult CreatePOST(FeatureSliderCreateViewModel viewModel)
        {
            //if (!Services.Authorizer.Authorize(Permissions.ManageMultiMenu, T("Not allowed to manage the menu")))
            //    return new HttpUnauthorizedResult();
            if (!ModelState.IsValid)
            {
                return View(viewModel);
            }

            try
            {
                if (null == viewModel || string.IsNullOrWhiteSpace(viewModel.Name))
                {
                    ModelState.AddModelError("Feature Slider", T("Feature Slider name must be supplied").ToString());

                    return View(new FeatureSliderCreateViewModel());
                }

                var featureSlider = Services.ContentManager.New("Slider");//Services.ContentManager.New("FeatureSlider");
                featureSlider.FeatureSliderName = viewModel.Name;
                featureSlider.AutoPlay = viewModel.AutoPlay;
                var model = Services.ContentManager.UpdateEditor(featureSlider.ContentItem, this);
                Services.ContentManager.Create(featureSlider, VersionOptions.Published);
                this._featureServiceSlider.CreateFeatureSlider(featureSlider);
            }
            catch (Exception exception)
            {
                Services.Notifier.Error(T("Adding feature slider failed: {0}", exception.Message));
                return View(new FeatureSliderCreateViewModel());
            }

            return RedirectToAction("Index");
        }

 

FeatureSliderService.cs

 

public void CreateFeatureSlider(FeatureSliderPart featureSlider)
      {
          var featureSliderPart = new FeatureSliderPartRecord
                                    {
                                        Id = featureSlider.Id,
                                        AutoPlay = featureSlider.AutoPlay,
                                        FeatureSliderName = featureSlider.FeatureSliderName
                                    };
          _featureSliderPartRepository.Create(featureSliderPart);
      }

 

public class FeatureSliderPartHandler : ContentHandler
    {
        public FeatureSliderPartHandler(IRepository<FeatureSliderPartRecord> repository)
        {
            Filters.Add(StorageFilter.For(repository));
        }
    }

 

 

public class FeatureSliderPart : ContentPart<FeatureSliderPartRecord>
    {
        public bool AutoPlay
        {
            get { return Record.AutoPlay; }
            set { Record.AutoPlay = value; }
        }

        public IEnumerable<SelectListItem> AvailableFeatureSliders { get; set; } // used on editor

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

        public bool HasAvailableSliders
        {
            get { return AvailableFeatureSliders != null && AvailableFeatureSliders.Count() > 0; }
        }

        //public DateTime LastUpdated
        //{
        //    get { return Record.LastUpdated; }
        //    set { Record.LastUpdated = value; }
        //}

        //public Plugins.MediaType SelectedType
        //{
        //    get { return (Plugins.MediaType)Record.MediaType; }
        //    set { Record.MediaType = (byte)value; }
        //}

        public byte SelectedPlugin
        {
            get { return Record.SelectedPlugin; }
            set { Record.SelectedPlugin = value; }
        }

        public string SelectedSlider { get; set; } // used on editor

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

        

        
    }

FeatureSliderPartRecord



 

public class FeatureSliderPartRecord : ContentPartRecord
    {
        public virtual bool AutoPlay { get; set; }

        public virtual string FeatureSliderName { get; set; }

        public virtual byte SelectedPlugin { get; set; }
    }

 

 

 

 I apologize for the formatting, but apparently CodePlex hates formatting.  I used the insert code and it looked fine until I tried to make text not in code black and bold.  And now the text is blue.  Anyway, any help is very much appreciated.  I am wholly lost as to where to go from here.  I read other posts, but nothing helped.  I copied code from other modules, but that did not help me either.

 

Coordinator
Jul 26, 2011 at 7:49 PM

FeatureSliderPart is a content part, but in the service, you are creating part records without a content item. I'd bet that's what the problem is.

Jul 29, 2011 at 4:26 PM
bertrandleroy wrote:

FeatureSliderPart is a content part, but in the service, you are creating part records without a content item. I'd bet that's what the problem is.


Thanks so much!  I absolutely missed that and that was the issue.  Something so simple I missed and I was staring at it for an hour at least trying to figure it out.  Probably should not have been trying to complete something at 1:40 in the morning :)  I am new to Orchard so I got a bit confused on some things (even after reading everything), but things are starting to fall in place.  I really like the architecture and its flexibility.