Item
可以包含多个Sizes
. 当我尝试为我的项目添加新尺寸时,它会引发NullReference
错误。当我尝试将图像添加到我的项目时,也会发生同样的事情。
你调用的对象是空的。
代码
var size = new Size(){
BasePrice = currentBasePrice, // not null, checked in debugger
DiscountPrice = currentDiscountPrice // not null, checked in debugger
};
// item is not null, checked in debugger
item.Sizes.Add(size); // nothing here is null, however it throws null reference error here
项目型号
public class Item
{
public int ID { get; set; }
public int CategoryID { get; set; }
virtual public Category Category { get; set; }
virtual public ICollection<Size> Sizes { get; set; }
virtual public ICollection<Image> Images { get; set; }
}
尺寸型号
public class Size
{
public int ID { get; set; }
public int ItemID { get; set; }
virtual public Item Item { get; set; } // tried to delete this, did not help
public decimal BasePrice { get; set; }
public decimal? DiscountPrice { get; set; }
}