0

Using uCommerce v6.6, Umbraco v7

I'm having trouble setting the category's display name and a custom definition that I have created.

I'm receiving this error:

not-null property references a null or transient value UCommerce.EntitiesV2.Category.ProductCatalog

I think this is b/c there's a property in the CategoryDescription class,

public virtual int CategoryDescriptionId { get; protected set; }

But I don't know how to set this b/c usually when you create objects like this, once you save an ID is created for you (think EF).

Also I'm needing to set the custom definition for the category, "productNumber".

var parentCategory = catalog.Categories.First(x => x.Name.Equals(parentName));

var newCategory = new Category
{
      Name = product.Name,
      Definition = productDef,
      DisplayOnSite = true,
      ParentCategory = parentCategory,
      ProductCatalog = catalog
};

catalog.Categories.Add(newCategory);
catalog.Save();


var catDescription = new CategoryDescription()
{
      DisplayName = product.GetValue<string>("productName"),
      Category = newCategory,
};

catDescription.Save();  // ****errors out here*****

var catProperty = new CategoryProperty()
{
     Category = newCategory,
     DefinitionField = DefinitionField.FirstOrDefault(x => x.Name.Equals("productNumber")),
     Value = product.GetValue<string>("productNumber"),
};

catProperty.Save();

All my variables have data, meaning they're not null. It's on the save that's the null. newCategory is successfully created as well each and every time.

Class def for CategoryDescription

public class CategoryDescription : IEntity
{
    public CategoryDescription();

    public static bool operator !=(CategoryDescription x, CategoryDescription y);
    public static bool operator ==(CategoryDescription x, CategoryDescription y);

    public virtual Category Category { get; set; }
    public virtual int CategoryDescriptionId { get; protected set; }
    public virtual int? ContentId { get; set; }
    public virtual string CultureCode { get; set; }
    public virtual string Description { get; set; }
    public virtual string DisplayName { get; set; }
    public virtual int Id { get; }
    public virtual bool RenderAsContent { get; set; }

    public static IQueryable<CategoryDescription> All();
    public virtual void Delete();
    public static void Delete(Expression<Func<CategoryDescription, bool>> expression);
    public override bool Equals(object obj);
    public static bool Exists(Expression<Func<CategoryDescription, bool>> expression);
    public static IList<CategoryDescription> Find(Expression<Func<CategoryDescription, bool>> expression);
    public static CategoryDescription FirstOrDefault(Expression<Func<CategoryDescription, bool>> expression);
    public static CategoryDescription Get(object id);
    public override int GetHashCode();
    public virtual void Save();
    public static CategoryDescription SingleOrDefault(Expression<Func<CategoryDescription, bool>> expression);
}
Rob Scott
  • 7,921
  • 5
  • 38
  • 63

1 Answers1

0

I recommend you use the method AddCategoryDescription on newCategory instance instead of trying to tie up the CategoryDescription with references manually. uCommerce is built on NHibernate and sometimes it can be difficult to find out which property is causing the trouble (As long you're not using stateless sessions; then you have to handle it).

I recall that uCommerce is set up to cascade all saves so if you call Save() at last on your catalog you should be good to go.

EDIT (To answer how to populate a property): You can populate a (new!) property value by using the following

var definitionField = DefinitionField.FirstOrDefault(x => !x.Deleted && x.Definition.Name == "MyDefinition");
var category = new Category();
category.AddProperty(new CategoryProperty
{
    Category = category,
    DefinitionField = definitionField,
    CultureCode = "en-GB",
    Value = "My value"
});

I haven't tested it but I sure it will work. If you want overwrite an existing property value you should find the CategoryProperty in the Category.CategoryProperties collection and then replace the Value property. Be aware if you create the same property twice since it can cause the backend YSOD (Unless they have fixed the unintended feature :) )

Best regards Martin

Martin
  • 123
  • 6
  • Appreciate the help - what about setting the custom definitions for the category? Setting product definitions are documented, but category defs are not. – Rob Scott May 21 '15 at 18:42
  • Just to make it clear - do you want to populate property data on a category through code or do you want to extend a category definition? – Martin May 22 '15 at 21:02
  • Populate a custom property (definition) of a category through code. – Rob Scott May 25 '15 at 13:13
  • Apologies for a bit late answer. I have updated my answer to contain how to populate a property. – Martin May 27 '15 at 15:56