使用 uCommerce v6.6、Umbraco v7
我在设置类别的显示名称和我创建的自定义定义时遇到问题。
我收到此错误:
非空属性引用空值或瞬态值 UCommerce.EntitiesV2.Category.ProductCatalog
我认为这是 b/c CategoryDescription 类中有一个属性,
public virtual int CategoryDescriptionId { get; protected set; }
但是我不知道如何设置这个 b/c 通常当你创建这样的对象时,一旦你保存了一个为你创建的 ID(想想 EF)。
此外,我需要为类别“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();
我所有的变量都有数据,这意味着它们不为空。保存的是null。newCategory 每次都成功创建。
CategoryDescription 的类 def
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);
}